Java.util.concurrent Synchronizers: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 11: Line 11:
==Overview==
==Overview==


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.
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 [[#Cyclic_Barrier|Cyclic Barrier]].


=Semaphore=
=Semaphore=

Revision as of 01:20, 25 January 2016

Internal

CountDownLatch

External

Overview

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.

Semaphore

CyclicBarrier