concurrent package classes
CountDownLatch Example
How to use?
1. new CountDownLatch with count
number
2. Pass CountDownLatch instance to
many threads for doing something
and counting down
3. Pass CountDownLatch instance to
one or many threads to wait for
counting down to zero
4. Thread will keep running after
counting down to zero
CountDownLatch Example
CylicBarrier
● Call await after work finished
● Thread will wait until all
thread finished
CylicBarrier example
Phaser
1. Register for each threads
2. Call arrive if job of current phase
finish
3. Call arriveAndAwaitAdvance if
you need wait for everyone
finished
4. Whenever all registered Phaser
arrived, phase will advance and
onAdvance method will be
called
Phaser example
Exchanger
Only one concept in Exchanger ⇒ exchange.
Used to exchange object between threads.
Exchanger Example
Reference: ExchangerMain.java
Semaphore
Number of permits to be acquired or released.
1. acquire: grab permit(s), wait until all available
2. tryAcquire: grab permit(s) until given timeout
3. release: release permit(s)
4. drainPermits: grab ALL permits
5. availablePermits: get current available permits count
Reference: Semaphore.java
Semaphore Example - TicketPool
Semaphore Example - TicketGrabber
Semaphore Example - TicketSellPoint and Main

Concurrent package classes