File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Is CountDownLatch Thread-Safe ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is CountDownLatch Thread-Safe ?" Watch "Is CountDownLatch Thread-Safe ?" New topic

Is CountDownLatch Thread-Safe ?

Joe Fisher

Joined: Nov 05, 2001
Posts: 12
If Thread wants to getCount() and conditionally countDown(), does block need to be synchronized ? These methods are not sych'd according to java docs

Henry Wong

Joined: Sep 28, 2004
Posts: 20525

Yes and no.... Yes, the countdown latch is thread safe and can be used by multiple threads. But no, the value return from getCount() is only valid til right before it returns. If you need to have data consistency between methods, you need to synchronize externally. (And this is true whether the individual methods are synchronized or not)

[ May 19, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Is CountDownLatch Thread-Safe ?
jQuery in Action, 3rd edition