Friday, September 26, 2014

EJ-69 CountDownLatch for thread barrier

Ref: Effective Java by Joshua Bloch 
Synchronizers are objects that eanble threads to wait for one another, allowing them to coordinate their activities. The most commonly used synchronizers are CountDownLatch and Semaphore.

Countdown latches are single-use barriers that allow one or more threads to wait for one or more other threads to do something. The sole constructor for CountDownLatch takes an int that is the number of times the countDown method must be invoked on the latch before all waiting threads are allowed to proceed.

Lets examine following delicious code, see numbered execution steps.

 For interval timing, always use System.nanoTime in preference to System.currentTimeMillis. System.nanoTime is both more accurate and more precise, and it is not affected by adjustments to the system's real-time clock.

No comments: