This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
First of all you must signal the resumed thread with condition.signal().
You remember about very important lock.unlock(s) in finally sections.
SCJP, SCWCD, SCBCD, SCDJWS
Joined: May 24, 2008
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?
Joined: Apr 15, 2009
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):