aspose file tools*
The moose likes Threads and Synchronization and the fly likes Question about working of ReentrantLock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Question about working of ReentrantLock" Watch "Question about working of ReentrantLock" New topic
Author

Question about working of ReentrantLock

D Preethi
Ranch Hand

Joined: May 24, 2008
Posts: 68
Hi All,

Can someone tell me what I am doing wrong here? The program seems to execute eternally(and does not print DONE) though I am changing the condition of the boolean variable in the thread. Basically, I want the main thread to resume once a condition is satisfied in the thread. How do I achieve that?




Thanks in advance,
Preethi
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.
First of all you must signal the resumed thread with condition.signal().
You remember about very important lock.unlock(s) in finally sections.

Adam



SCJP, SCWCD, SCBCD, SCDJWS
D Preethi
Ranch Hand

Joined: May 24, 2008
Posts: 68
Thanks a lot, Adam!! Currently, there is no way to get the list of all available conditions on a particular lock, right? I just see this API -

Condition c = lock.newCondition();

So, if I want to split the above mentioned code in different classes ( the sleep thread in one class, and the variable monitoring code in another class), I shall need to pass the references to both lock and the condition the first class created, right?
Adam Smolnik
Ranch Hand

Joined: Apr 15, 2009
Posts: 63
Hey.

Essentially, I personally would NOT make access locks and conditions outside class without very justified reason.
Next, I would browse existing solutions in java.util.concurrent package like Executors, CyclicBarrier etc..

In case of need, you consider my somewhat other approach to your described problem(semantically should be equivalent):



Results:

Hey there
var = false
var = true
DONE!!!

Adam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about working of ReentrantLock