aspose file tools*
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
Author

Is CountDownLatch Thread-Safe ?

Joe Fisher
Greenhorn

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

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19092
    
  40

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)

Henry
[ 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: http://aspose.com/file-tools
 
subject: Is CountDownLatch Thread-Safe ?