Java.util.concurrent Synchronizers: Difference between revisions
No edit summary |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | |||
* https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/package-summary.html | |||
=Internal= | =Internal= | ||
* [[java.util.concurrent# | * [[java.util.concurrent#Synchronizers|java.util.concurrent]] | ||
=CountDownLatch= | =CountDownLatch= | ||
{{External|https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html}} | |||
A <tt>CountDownLatch</tt> is a synchronization tool that allows one or more threads to wait on it until a certain number of operations being performed in other threads completes. The latch is initialized with a count. Any thread calling <tt>await()</tt> blocks unit the count reaches zero, after which it is released. You can think of those threads as waiting on a gate to open. The count is decremented with <tt>countDown()</tt>, which can be called in a loop from a single thread, or concurrently from multiple threads. Note that the threads invoking <tt>countDown()</tt> are not blocked on the latch - they decrement and continue. | A <tt>CountDownLatch</tt> is a synchronization tool that allows one or more threads to wait on it until a certain number of operations being performed in other threads completes. The latch is initialized with a count. Any thread calling <tt>await()</tt> blocks unit the count reaches zero, after which it is released. You can think of those threads as waiting on a gate to open. The count is decremented with <tt>countDown()</tt>, which can be called in a loop from a single thread, or concurrently from multiple threads. Note that the threads invoking <tt>countDown()</tt> are not blocked on the latch - they decrement and continue. | ||
Once the count reached zero, the latch cannot be reused. If you need a reusable mechanism, use [[# | Once the count reached zero, the latch cannot be reused. If you need a reusable mechanism, use [[#CyclicBarrier|cyclic barrier]]. | ||
=CyclicBarrier= | =CyclicBarrier= | ||
Line 23: | Line 20: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
int threadCount = ...; | |||
final CyclicBarrier barrier = new CyclicBarrier(threadCount, | final CyclicBarrier barrier = new CyclicBarrier(threadCount, () -> System.out.print("ALL threads have finished")); | ||
for(int i = 0; i < threadCount; i ++) { | for(int i = 0; i < threadCount; i ++) { | ||
new Thread( | new Thread(() -> { | ||
// do stuff ... | // do stuff ... | ||
Line 42: | Line 32: | ||
try { | try { | ||
barrier.await(); | |||
} | } | ||
catch(Exception e) { | catch(Exception e) { | ||
Line 48: | Line 38: | ||
log.error("barrier error", e); | log.error("barrier error", e); | ||
} | } | ||
}, "Thread #" + i).start(); | |||
}, "Thread #" + i).start(); | } | ||
</syntaxhighlight> | |||
=Semaphore= | |||
... | =Exchanger= | ||
{{External|https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/Exchanger.html}} | |||
A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its partner's object on return. An Exchanger may be viewed as a bidirectional form of a SynchronousQueue. |
Latest revision as of 01:29, 27 October 2018
External
Internal
CountDownLatch
A CountDownLatch is a synchronization tool that allows one or more threads to wait on it until a certain number of operations being performed in other threads completes. The latch is initialized with a count. Any thread calling await() blocks unit the count reaches zero, after which it is released. You can think of those threads as waiting on a gate to open. The count is decremented with countDown(), which can be called in a loop from a single thread, or concurrently from multiple threads. Note that the threads invoking countDown() are not blocked on the latch - they decrement and continue.
Once the count reached zero, the latch cannot be reused. If you need a reusable mechanism, use cyclic barrier.
CyclicBarrier
A cyclic barrier is a primitive useful when waiting for all threads in a group to finish work they are doing individually.
int threadCount = ...;
final CyclicBarrier barrier = new CyclicBarrier(threadCount, () -> System.out.print("ALL threads have finished"));
for(int i = 0; i < threadCount; i ++) {
new Thread(() -> {
// do stuff ...
try {
barrier.await();
}
catch(Exception e) {
log.error("barrier error", e);
}
}, "Thread #" + i).start();
}
Semaphore
Exchanger
A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its partner's object on return. An Exchanger may be viewed as a bidirectional form of a SynchronousQueue.