Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Hi Maria, Here's what I think is happening: After line 6 executes we have two threads in a runnable state: the main one (thread main) and the one executing the run method of class A (thread 0). By means of lines 1 and 7 they both fight for the lock on String array sa which at this point (after line 6) is a->"Not Done", a->"X", a->"Y", a->"Z" . Scenario 1 Thread 0 gets the lock first, line 1 executes before line 7. Compares sa with "Done" which results in false. Enters the while loop and executes the wait method of the sa object. The wait method makes the calling thread unlock all it's locks (including the lock on sa) and puts the thread to "sleep" but unlike the sleep method doesn't keep the locks. While thread 0 sleeps thread main who in the main time was blocked waiting at line 7 suddenly finds the sa String array unlocked, locks it and executes lines 8,9,10,11,12 and exits the block. Now sa is: a->"Done", a->"A", a->"B", a->"C" Line 12 sends a notification to one of the threads (in this case only one, thread 0) waiting for the sa object and wakes up a thread (here thread 0). Thread 0 wakes up, executes line 2, finds the expression false and exits the loop and executes line 4 printing ABC. Scenario 2 Thread main gets the lock first, line 7 executes before line 1. Thread main locks sa, executes lines 8,9,10,11,12 and exits the block. Thread 0 finds the while expression on line 2 false and doesn't even bother to enter the while loop but goes directly to line 4 and prints ABC. Hope this was helpful and I hope I didn't oversee anything.
Just in case : wait will release only the monitor associated with the object on which wait was called on. If the thread calling wait holds more locks on other objects, they are still owned by that thread after its call to wait. In the example the second thread is not allowed to proceed untill main ends the block synchronized on lock1; regardless main thread (t) waits on lock2. Note: well t is not used in this example, but it was in my previous one.
[ January 29, 2003: Message edited by: Jose Botella ] [ January 29, 2003: Message edited by: Jose Botella ]