Hi all
I�m preparing for the Programmer Exam, and have been reading the good book:
Sun Certified Programmer & Developer for Java2 STUDY GUIDE by Kathy Sierra and Bert Bates.
Today I started on chapter 9 (Threads), and encountered a example I don�t really get.
The example is on page 529, and covers the wait() and notify() methods.
This code compiles and runs just fine, but in my opinion there is a big flaw. In the beginning of the chapter you can read over and over again that
thread stuff is not guaranteed.
So how come that the authors assume the thread behaviour in the preceding example?
The example relays on the fact that the first thread (main method in class ThreadA) continues execution and reach the synchronized(b){ (and obtaining the lock on b) BEFORE the synchronized(this){ (in class ThreadB) runs.
For if b runs first and reaches the synchronized(this){ then it gets the lock on itself, thus the main thread will block for the synchronized(b){, and when it gets the lock (when b has ended) then it calls the b.wait(), but will never be notified because b has already run the notify();
Please confirm my statement!
I hope you understand what I mean!? And excuse my bad English, for it�s not my native language.
Merry Christmas and Happy New Year!!!
RK