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: 18120
    
  39

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)
 
Don't get me started about those stupid light bulbs.
 
subject: Is CountDownLatch Thread-Safe ?
 
Similar Threads
Thread and Object
subject
Thread & Synchronization
WA #1.....word association
Tech Word Game