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
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply 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: 16815
    
  19

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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Is CountDownLatch Thread-Safe ?
 
Similar Threads
Thread & Synchronization
Thread and Object
Tech Word Game
subject
WA #1.....word association