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.
The dynamic you're thinking of here applies to the suspend/resume scheme that was once available in Java. In that scheme, a suspended thread could not continue unless it was resumed by another thread or by the system.
The wait/notify scheme in Java closely models what is called a conditional variable lock in multithreaded programming. Suffice it to say here that if a thread waits and there is no one to notify it, the system will eventually allow the thread to run again to see if the condition (!done) has been satisfied.
The full explanation is complex; I would need a bit of time to figure a short way to explain it.
But since we cannot be sure that threads would produce same output on different machines.....how could we know the precise answer. Rohit your code for me printed 0 and waited infinitely. [ October 02, 2005: Message edited by: Sandeep Chhabra ]
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Here is a slightly modified version that actually calls notify to wake the sleeping thread.
I also synchronized access to the 'done' variable as per Effective Java Item #48; otherwise thread a1 may never see the update from thread main if you are running on a multi-cpu system. [ October 02, 2005: Message edited by: Sid Remey ]