This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 18506
    
  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)
 
Don't get me started about those stupid light bulbs.
 
subject: Is CountDownLatch Thread-Safe ?
 
Similar Threads
Tech Word Game
WA #1.....word association
subject
Thread and Object
Thread & Synchronization