-
Notifications
You must be signed in to change notification settings - Fork 144
/
listings.html
385 lines (343 loc) · 30.1 KB
/
listings.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Java Concurrency in Practice - Code Listings</title>
<meta name="description" content="JCiP code listings">
<meta name="author" content="Tim Peierls">
<meta name="author" content="Brian Goetz">
<link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" href="stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="stylesheets/bootstrap-theme.min.css">
<link rel="stylesheet" href="stylesheets/jcip-theme.css">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
<![endif]-->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-38133818-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-38133818-1');
</script>
<style>
#main li {
height: 28px;
margin-top: 12px;
}
#main li.yuk::after {
position: absolute;
content: url('images/mryuk.gif');
}
</style>
</head>
<body role="document">
<header class="navbar navbar-default navbar-fixed-top">
<!-- loaded dynamically -->
</header>
<div id="main" class="container" role="main">
<div class="page-header">
<h1>Code Listings</h1>
</div>
<p class="alert alert-info">
Written by Brian Goetz and Tim Peierls with assistance from members of
JCP JSR-166 Expert Group and released to the public domain, as explained by the
<a href="http://creativecommons.org/licenses/publicdomain">Creative Commons public domain license</a>.
</p>
<p class="alert alert-warning">
Note that this license is no longer recommended by Creative Commons for any use, and
has never been recommended for use with software.
Nevertheless, this is the license that we picked in 2006.
</p>
<!-- page body -->
<h2>Introduction</h2>
<ul class="list-unstyled">
<li >1.1 <a href="listings/UnsafeSequence.java">Non-thread-safe sequence generator.</a></li>
<li >1.2 <a href="listings/Sequence.java">Thread-safe sequence generator.</a></li>
</ul>
<h2>Thread Safety</h2>
<ul class="list-unstyled">
<li >2.1 <a href="listings/StatelessFactorizer.java">A stateless servlet.</a></li>
<li class="yuk">2.2 <a href="listings/UnsafeCountingFactorizer.java">Servlet that counts requests without the necessary
synchronization.</a></li>
<li class="yuk">2.3 <a href="listings/LazyInitRace.java">Race condition in lazy initialization.</a></li>
<li >2.4 <a href="listings/CountingFactorizer.java">Servlet that counts requests using AtomicLong.<a/></li>
<li class="yuk">2.5 <a href="listings/UnsafeCachingFactorizer.java">Servlet that attempts to cache its last result
without adequate atomicity.</a></li>
<li class="yuk">2.6 <a href="listings/SynchronizedFactorizer.java">Servlet that caches last result, but with unnacceptably
poor concurrency.</a></li>
<li >2.7 <a href="listings/NonReentrantDeadlock.java">Code that would deadlock if intrinsic locks were
not reentrant.</a></li>
<li >2.8 <a href="listings/CachedFactorizer.java">Servlet that caches its last request and result.</a></li>
</ul>
<h2>Sharing Objects</h2>
<ul class="list-unstyled">
<li class="yuk">3.1 <a href="listings/NoVisibility.java">Sharing variables without synchronization.</a></li>
<li >3.2 <a href="listings/MutableInteger.java">Non-thread-safe mutable integer holder.</a></li>
<li >3.3 <a href="listings/SynchronizedInteger.java">Thread-safe mutable integer holder.</a></li>
<li >3.4 <a href="listings/CountingSheep.java">Counting sheep.</a></li>
<li >3.5 <a href="listings/Secrets.java">Publishing an object.</a></li>
<li class="yuk">3.6 <a href="listings/UnsafeStates.java">Allowing internal mutable state to escape.</a></li>
<li class="yuk">3.7 <a href="listings/ThisEscape.java">Implicitly allowing the this reference to escape.</a></li>
<li >3.8 <a href="listings/SafeListener.java">Using a factory method to prevent the this reference from escaping during construction.</a></li>
<li >3.9 <a href="listings/Animals.java">Thread confinement of local primitive and reference variables.</a></li>
<li >3.10 <a href="listings/ConnectionDispenser.java">Using ThreadLocal to ensure thread confinement.</a></li>
<li >3.11 <a href="listings/ThreeStooges.java">Immutable class built out of mutable underlying objects.</a></li>
<li >3.12 <a href="listings/OneValueCache.java">Immutable holder for caching a number and its factors.</a></li>
<li >3.13 <a href="listings/VolatileCachedFactorizer.java">Caching the last result using a volatile reference to an immutable holder object.</a></li>
<li class="yuk">3.14 <a href="listings/StuffIntoPublic.java">Publishing an object without adequate synchronization.</a></li>
<li >3.15 <a href="listings/Holder.java">Class at risk of failure if not properly published.</a></li>
</ul>
<h2>Composing Objects</h2>
<ul class="list-unstyled">
<li >4.1 <a href="listings/Counter.java">Simple thread-safe counter using the Java monitor pattern.</a></li>
<li >4.2 <a href="listings/PersonSet.java">Using confinement to ensure thread safety.</a></li>
<li >4.3 <a href="listings/PrivateLock.java">Guarding state with a private lock.</a></li>
<li >4.4 <a href="listings/MonitorVehicleTracker.java">Monitor-based vehicle tracker implementation.</a></li>
<li >4.5 <a href="listings/MutablePoint.java">Mutable point class similar to java.awt.Point.</a></li>
<li >4.6 <a href="listings/Point.java">Immutable Point class used by DelegatingVehicleTracker.</a></li>
<li >4.7 <a href="listings/DelegatingVehicleTracker.java">Delegating thread safety to a ConcurrentHashMap.</a></li>
<li >4.8 <a href="listings/DelegatingVehicleTracker.java">Returning a static copy of the location set instead of a 'live' one.</a></li>
<li >4.9 <a href="listings/VisualComponent.java">Delegating thread safety to multiple underlying state variables.</a></li>
<li class="yuk">4.10 <a href="listings/NumberRange.java">Number range class that does not sufficiently protect its invariants.</a></li>
<li >4.11 <a href="listings/SafePoint.java">Thread-safe mutable point class.</a></li>
<li >4.12 <a href="listings/PublishingVehicleTracker.java">Vehicle tracker that safely publishes underlying state.</a></li>
<li >4.13 <a href="listings/BetterVector.java">Extending Vector to have a put-if-absent method.</a></li>
<li class="yuk">4.14 <a href="listings/ListHelpers.java">Non-thread-safe attempt to implement put-if-absent.</a></li>
<li >4.15 <a href="listings/ListHelpers.java">Implementing put-if-absent with client-side locking.</a></li>
<li >4.16 <a href="listings/ImprovedList.java">Implementing put-if-absent using composition.</a></li>
</ul>
<h2>Building Blocks</h2>
<ul class="list-unstyled">
<li > 5.1 <a href="listings/UnsafeVectorHelpers.java">Compound actions on a Vector that may produce confusing results.</a></li>
<li > 5.2 <a href="listings/SafeVectorHelpers.java">Compound actions on Vector using client-side locking.</a></li>
<li > 5.3 Iteration that may throw ArrayIndexOutOfBoundsException. (fragment)</li>
<li > 5.4 Iteration with client-side locking. (fragment)</li>
<li > 5.5 Iterating a List with an Iterator. (fragment)</li>
<li class="yuk"> 5.6 <a href="listings/HiddenIterator.java">Iteration hidden within string concatenation.</a></li>
<li > 5.7 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentMap.html#method_summary">
ConcurrentMap interface.</a> (external link to Javadoc)</li>
<li > 5.8 <a href="listings/ProducerConsumer.java">Producer and consumer tasks in a desktop search application.</a></li>
<li > 5.9 <a href="listings/ProducerConsumer.java">Starting the desktop search.</a></li>
<li > 5.10 <a href="listings/TaskRunnable.java">Restoring the interrupted status so as not to swallow the interrupt.</a></li>
<li > 5.11 <a href="listings/TestHarness.java">Using CountDownLatch for starting and stopping threads in timing tests.</a></li>
<li > 5.12 <a href="listings/Preloader.java">Using FutureTask to preload data that is needed later.</a></li>
<li > 5.13 <a href="listings/LaunderThrowable.java">Coercing an unchecked Throwable to a RuntimeException.</a></li>
<li > 5.14 <a href="listings/BoundedHashSet.java">Using Semaphore to bound a collection.</a></li>
<li > 5.15 <a href="listings/CellularAutomata.java">Coordinating computation in a cellular automaton with CyclicBarrier.</a></li>
<li > 5.16 <a href="listings/Memoizer1.java">Initial cache attempt using HashMap and synchronization.</a></li>
<li > 5.17 <a href="listings/Memoizer2.java">Replacing HashMap with ConcurrentHashMap.</a></li>
<li > 5.18 <a href="listings/Memoizer2.java">Memoizing wrapper using FutureTask.</a></li>
<li > 5.19 <a href="listings/Memoizer.java">Final implementation of Memoizer.</a></li>
<li > 5.20 <a href="listings/Factorizer.java">Factorizing servlet that caches results using Memoizer.</a></li>
</ul>
<h2>Task Execution</h2>
<ul class="list-unstyled">
<li > 6.1 <a href="listings/SingleThreadWebServer.java">Sequential web server. </a></li>
<li > 6.2 <a href="listings/ThreadPerTaskWebServer.java">Web server that starts a new thread for each request.</a></li>
<li > 6.3 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executor.html#method_summary">
Executor interface.</a> (external link to Javadoc) </li>
<li > 6.4 <a href="listings/TaskExecutionWebServer.java">Web server using a thread pool.</a></li>
<li > 6.5 <a href="listings/ThreadPerTaskExecutor.java">Executor that starts a new thread for each task.</a></li>
<li > 6.6 <a href="listings/WithinThreadExecutor.java">Executor that executes tasks synchronously in the calling thread.</a></li>
<li > 6.7 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#method_summary">
Lifecycle methods in ExecutorService.</a> (external link to Javadoc, shows <em>all</em> methods, not just lifecycle methods) </li>
<li > 6.8 <a href="listings/LifecycleWebServer.java">Web server with shutdown support.</a></li>
<li > 6.9 <a href="listings/OutOfTime.java">Class illustrating confusing Timer behavior.</a></li>
<li > 6.10 <a href="listings/SingleThreadRenderer.java">Rendering page elements sequentially.</a></li>
<li > 6.11 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Callable.html#method_summary">
Callable</a> and <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html#method_summary">
Future</a> interfaces. (external links to Javadoc) </li>
<li > 6.12 Default implementation of newTaskFor in ThreadPoolExecutor. (See JDK source) </li>
<li > 6.13 <a href="listings/FutureRenderer.java">Waiting for image download with Future.</a></li>
<li > 6.14 QueueingFuture class used by ExecutorCompletionService. (See JDK source) </li>
<li > 6.15 <a href="listings/Renderer.java">Using CompletionService to render page elements as they become available.</a></li>
<li > 6.16 <a href="listings/RenderWithTimeBudget.java">Fetching an advertisement with a time budget.</a></li>
<li > 6.17 <a href="listings/TimeBudget.java">Requesting travel quotes under a time budget.</a></li>
</ul>
<h2>Cancellation and Shutdown</h2>
<ul class="list-unstyled">
<li > 7.1 <a href="listings/PrimeGenerator.java">Using a volatile field to hold cancellation state.</a></li>
<li > 7.2 <a href="listings/PrimeGenerator.java">Generating a second's worth of prime numbers.</a></li>
<li class="yuk"> 7.3 <a href="listings/BrokenPrimeProducer.java">Unreliable cancellation that can leave producers stuck in a blocking operation.</a></li>
<li > 7.4 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#method_summary">
Interruption methods in Thread.</a> (external link to Javadoc, shows <em>all</em> <tt>Thread</tt> methods, not just interruption-related) </li>
<li > 7.5 <a href="listings/PrimeProducer.java">Using interruption for cancellation.</a></li>
<li > 7.6 Propagating InterruptedException to callers. (fragment) </li>
<li > 7.7 <a href="listings/NoncancelableTask.java">Noncancelable task that restores interruption before exit.</a></li>
<li class="yuk"> 7.8 <a href="listings/TimedRun1.java">Scheduling an interrupt on a borrowed thread.</a></li>
<li > 7.9 <a href="listings/TimedRun2.java">Interrupting a task in a dedicated thread.</a></li>
<li > 7.10 <a href="listings/TimedRun.java">Cancelling a task using Future.</a></li>
<li > 7.11 <a href="listings/ReaderThread.java">Encapsulating nonstandard cancellation in a Thread by overriding interrupt.</a></li>
<li > 7.12 <a href="listings/SocketUsingTask.java">Encapsulating nonstandard cancellation in a task with newTaskFor. </a></li>
<li > 7.13 <a href="listings/LogWriter.java">Producer-consumer logging service with no shutdown support.</a></li>
<li > 7.14 Unreliable way to add shutdown support to the logging service. (fragment) </li>
<li > 7.15 <a href="listings/LogService.java">Adding reliable cancellation to LogWriter.</a></li>
<li > 7.16 Logging service that uses an ExecutorService. </li>
<li > 7.17 <a href="listings/IndexingService.java">Shutdown with poison pill.</a></li>
<li > 7.18 <a href="listings/IndexingService.java">Producer thread for IndexingService.</a></li>
<li > 7.19 <a href="listings/IndexingService.java">Consumer thread for IndexingService.</a></li>
<li > 7.20 <a href="listings/CheckForMail.java">Using a private Executor whose lifetime is bounded by a method call.</a></li>
<li > 7.21 <a href="listings/TrackingExecutor.java">ExecutorService that keeps track of cancelled tasks after shutdown.</a></li>
<li > 7.22 <a href="listings/WebCrawler.java">Using TrackingExecutorService to save unfinished tasks for later execution.</a></li>
<li > 7.23 Typical thread-pool worker thread structure. (fragment) </li>
<li > 7.24 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html#method_summary">
UncaughtExceptionHandler interface.</a> (external link to Javadoc) </li>
<li > 7.25 <a href="listings/UEHLogger.java">UncaughtExceptionHandler that logs the exception.</a></li>
<li > 7.26 Registering a shutdown hook to stop the logging service. (fragment) </li>
</ul>
<h2>Applying Thread Pools</h2>
<ul class="list-unstyled">
<li class="yuk"> 8.1 <a href="listings/ThreadDeadlock.java">Task that deadlocks in a single-threaded Executor.</a></li>
<li > 8.2 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html#ThreadPoolExecutor(int,%20int,%20long,%20java.util.concurrent.TimeUnit,%20java.util.concurrent.BlockingQueue,%20java.util.concurrent.ThreadFactory,%20java.util.concurrent.RejectedExecutionHandler)">
General constructor for ThreadPoolExecutor.</a> (external link to Javadoc) </li>
<li > 8.3 Creating a fixed-sized thread pool with a bounded queue and the caller-runs saturation policy. (fragment) </li>
<li > 8.4 <a href="listings/BoundedExecutor.java">Using a Semaphore to throttle task submission.</a></li>
<li > 8.5 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ThreadFactory.html#method_summary">
ThreadFactory interface.</a> (external link to Javadoc) </li>
<li > 8.6 <a href="listings/MyThreadFactory.java">Custom thread factory.</a></li>
<li > 8.7 <a href="listings/MyAppThread.java">Custom thread base class.</a></li>
<li > 8.8 Modifying an Executor created with the standard factories. (fragment) </li>
<li > 8.9 <a href="listings/TimingThreadPool.java">Thread pool extended with logging and timing.</a></li>
<li > 8.10 <a href="listings/TransformingSequential.java">Transforming sequential execution into parallel execution.</a></li>
<li > 8.11 <a href="listings/TransformingSequential.java">Transforming sequential tail-recursion into parallelized recursion.</a></li>
<li > 8.12 <a href="listings/TransformingSequential.java">Waiting for results to be calculated in parallel.</a></li>
<li > 8.13 <a href="listings/Puzzle.java">Abstraction for puzzles like the 'sliding blocks puzzle'.</a></li>
<li > 8.14 <a href="listings/PuzzleNode.java">Link node for the puzzle solver framework.</a></li>
<li > 8.15 <a href="listings/SequentialPuzzleSolver.java">Sequential puzzle solver.</a></li>
<li > 8.16 <a href="listings/ConcurrentPuzzleSolver.java">Concurrent version of puzzle solver.</a></li>
<li > 8.17 <a href="listings/ValueLatch.java">Result-bearing latch used by ConcurrentPuzzleSolver.</a></li>
<li > 8.18 <a href="listings/PuzzleSolver.java">Solver that recognizes when no solution exists.</a></li>
</ul>
<h2>GUI Applications</h2>
<ul class="list-unstyled">
<li > 9.1 <a href="listings/SwingUtilities.java">Implementing SwingUtilities using an Executor.</a></li>
<li > 9.2 <a href="listings/GuiExecutor.java">Executor built atop SwingUtilities.</a></li>
<li > 9.3 Simple event listener. (fragment) </li>
<li > 9.4 <a href="listings/ListenerExamples.java">Binding a long-running task to a visual component.</a></li>
<li > 9.5 <a href="listings/ListenerExamples.java">Long-running task with user feedback.</a></li>
<li > 9.6 <a href="listings/ListenerExamples.java">Cancelling a long-running task.</a></li>
<li > 9.7 <a href="listings/BackgroundTask.java">Background task class supporting cancellation, completion notification, and progress notification.</a></li>
<li > 9.8 <a href="listings/ListenerExamples.java">Initiating a long-running, cancellable task with BackgroundTask.</a></li>
<h2>Avoiding Liveness Hazards</h2>
<li class="yuk"> 10.1 <a href="listings/LeftRightDeadlock.java">Simple lock-ordering deadlock.</a></li>
<li class="yuk"> 10.2 <a href="listings/DynamicOrderDeadlock.java">Dynamic lock-ordering deadlock.</a></li>
<li > 10.3 <a href="listings/InduceLockOrder.java">Inducing a lock ordering to avoid deadlock.</a></li>
<li > 10.4 <a href="listings/DemonstrateDeadlock.java">Driver loop that induces deadlock under typical conditions.</a></li>
<li class="yuk"> 10.5 <a href="listings/CooperatingDeadlock.java">Lock-ordering deadlock between cooperating objects.</a></li>
<li > 10.6 <a href="listings/CooperatingNoDeadlock.java">Using open calls to avoiding deadlock between cooperating objects.</a></li>
<li > 10.7 Portion of thread dump after deadlock. (not a code listing) </li>
</ul>
<h2>Performance and Scalability</h2>
<ul class="list-unstyled">
<li > 11.1 <a href="listings/WorkerThread.java">Serialized access to a task queue.</a></li>
<li class="yuk"> 11.2 Synchronization that has no effect. (fragment) </li>
<li > 11.3 <a href="listings/ThreeStooges.java">Candidate for lock elision.</a></li>
<li > 11.4 <a href="listings/AttributeStore.java">Holding a lock longer than necessary.</a></li>
<li > 11.5 <a href="listings/BetterAttributeStore.java">Reducing lock duration.</a></li>
<li > 11.6 <a href="listings/ServerStatusBeforeSplit.java">Candidate for lock splitting.</a></li>
<li > 11.7 <a href="listings/ServerStatusAfterSplit.java">ServerStatus refactored to use split locks.</a></li>
<li > 11.8 <a href="listings/StripedMap.java">Hash-based map using lock striping.</a></li>
</ul>
<h2>Testing Concurrent Programs</h2>
<ul class="list-unstyled">
<li > 12.1 <a href="listings/SemaphoreBoundedBuffer.java">Bounded buffer using Semaphore.</a></li>
<li > 12.2 <a href="listings/TestBoundedBuffer.java">Basic unit tests for BoundedBuffer.</a></li>
<li > 12.3 <a href="listings/TestBoundedBuffer.java">Testing blocking and responsiveness to interruption.</a></li>
<li > 12.4 <a href="listings/XorShift.java">Medium-quality random number generator suitable for testing.</a></li>
<li > 12.5 <a href="listings/PutTakeTest.java">Producer-consumer test program for BoundedBuffer.</a></li>
<li > 12.6 <a href="listings/PutTakeTest.java">Producer and consumer classes used in PutTakeTest.</a></li>
<li > 12.7 <a href="listings/TestBoundedBuffer.java">Testing for resource leaks.</a></li>
<li > 12.8 <a href="listings/TestThreadPool.java">Thread factory for testing ThreadPoolExecutor.</a></li>
<li > 12.9 <a href="listings/TestThreadPool.java">Test method to verify thread pool expansion.</a></li>
<li > 12.10 Using Thread.yield to generate more interleavings. (fragment) </li>
<li > 12.11 <a href="listings/BarrierTimer.java">Barrier-based timer.</a></li>
<li > 12.12 <a href="listings/TimedPutTakeTest.java">Testing with a barrier-based timer.</a></li>
<li > 12.13 <a href="listings/TimedPutTakeTest.java">Driver program for TimedPutTakeTest.</a></li>
</ul>
<h2>Explicit Locks</h2>
<ul class="list-unstyled">
<li > 13.1 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/Lock.html#method_summary">
Lock interface.</a> (external link to Javadoc) </li>
<li > 13.2 Guarding object state using ReentrantLock. (fragment) </li>
<li > 13.3 <a href="listings/DeadlockAvoidance.java">Avoiding lock-ordering deadlock using try Lock.</a></li>
<li > 13.4 <a href="listings/TimedLocking.java">Locking with a time budget.</a></li>
<li > 13.5 <a href="listings/InterruptibleLocking.java">Interruptible lock acquisition.</a></li>
<li > 13.6 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReadWriteLock.html#method_summary">
ReadWriteLock interface.</a> (external link to Javadoc) </li>
<li > 13.7 <a href="listings/ReadWriteMap.java">Wrapping a Map with a read-write lock.</a></li>
</ul>
<h2>Building Custom Synchronizers</h2>
<ul class="list-unstyled">
<li > 14.1 Structure of blocking state-dependent actions. (fragment) </li>
<li > 14.2 <a href="listings/BaseBoundedBuffer.java">Base class for bounded buffer implementations.</a></li>
<li > 14.3 <a href="listings/GrumpyBoundedBuffer.java">Bounded buffer that balks when preconditions are not met.</a></li>
<li > 14.4 <a href="listings/GrumpyBoundedBuffer.java">Client logic for calling GrumpyBoundedBuffer.</a></li>
<li > 14.5 <a href="listings/SleepyBoundedBuffer.java">Bounded buffer using crude blocking.</a></li>
<li > 14.6 <a href="listings/BoundedBuffer.java">Bounded buffer using condition queues.</a></li>
<li > 14.7 Canonical form for state-dependent methods. (fragment) </li>
<li > 14.8 <a href="listings/BoundedBuffer.java">Using conditional notification in BoundedBuffer.put.</a></li>
<li > 14.9 <a href="listings/ThreadGate.java">Recloseable gate using wait and notifyAll.</a></li>
<li > 14.10 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/Condition.html#method_summary">
Condition interface.</a> (external link to Javadoc) </li>
<li > 14.11 <a href="listings/ConditionBoundedBuffer.java">Bounded buffer using explicit condition variables.</a></li>
<li > 14.12 <a href="listings/SemaphoreOnLock.java">Counting semaphore implemented using Lock.</a></li>
<li > 14.13 Canonical forms for acquisition and release in AQS. (fragment) </li>
<li > 14.14 <a href="listings/OneShotLatch.java">Binary latch using AbstractQueuedSynchronizer.</a></li>
<li > 14.15 tryAcquire implementation from nonfairReentrantLock. (See JDK source) </li>
<li > 14.16 tryAcquireShared and tryReleaseShared from Semaphore. (See JDK source) </li>
</ul>
<h2>Atomic Variables and Nonblocking Synchronization</h2>
<ul class="list-unstyled">
<li > 15.1 <a href="listings/SimulatedCAS.java">Simulated CAS operation.</a></li>
<li > 15.2 <a href="listings/CasCounter.java">Nonblocking counter using CAS.</a></li>
<li > 15.3 <a href="listings/CasNumberRange.java">Preserving multivariable invariants using CAS.</a></li>
<li > 15.4 <a href="listings/ReentrantLockPseudoRandom.java">Random number generator using ReentrantLock.</a></li>
<li > 15.5 <a href="listings/AtomicPseudoRandom.java">Random number generator using AtomicInteger.</a></li>
<li > 15.6 <a href="listings/ConcurrentStack.java">Nonblocking stack using Treiber's algorithm.</a></li>
<li > 15.7 <a href="listings/LinkedQueue.java">Insertion in the Michael-Scott nonblocking queue algorithm.</a></li>
<li > 15.8 Using atomic field updaters in ConcurrentLinkedQueue. (fragment) </li>
</ul>
<h2>The Java Memory Model</h2>
<ul class="list-unstyled">
<li class="yuk"> 16.1 <a href="listings/PossibleReordering.java">Insufficiently synchronized program that can have surprising results.</a></li>
<li > 16.2 Inner class of FutureTask illustrating synchronization piggybacking. (See JDK source) </li>
<li class="yuk"> 16.3 <a href="listings/UnsafeLazyInitialization.java">Unsafe lazy initialization.</a></li>
<li > 16.4 <a href="listings/SafeLazyInitialization.java">Thread-safe lazy initialization.</a></li>
<li > 16.5 <a href="listings/EagerInitialization.java">Eager initialization.</a></li>
<li > 16.6 <a href="listings/ResourceFactory.java">Lazy initialization holder class idiom.</a></li>
<li class="yuk"> 16.7 <a href="listings/DoubleCheckedLocking.java">Double-checked-locking antipattern.</a></li>
<li > 16.8 <a href="listings/SafeStates.java">Initialization safety for immutable objects.</a></li>
</ul>
</div><!-- /.container -->
<footer class="footer navbar">
<!-- loaded dynamically -->
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!--
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
-->
<script src="javascripts/bootstrap.min.js"></script>
<script src="javascripts/jcip-main.js"></script>
</body>
</html>