CountDownLatch in Java
1. Description
A java.util.concurrent.CountDownLatch is a concurrency construct that allows one or more threads to wait for a given set of operations to complete.
A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times
3. Its methods
void |
await() Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted . |
boolean |
await(long timeout, TimeUnit unit) Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted , or the specified waiting time elapses. |
void |
countDown() Decrements the count of the latch, releasing all waiting threads if the count reaches zero. |
long |
getCount() Returns the current count. |
String |
toString() Returns a string identifying this latch, as well as its state. |
3.Sample usage:
*The first is a start signal that prevents any worker from proceeding until the driver is ready for them to proceed;
*The second is a completion signal that allows the driver to wait until all workers have completed.












































4. Reference
api:http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html
posted on 2011-05-07 15:10 XXXXXX 閱讀(259) 評論(0) 編輯 收藏 所屬分類: Programing