This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Instead of catching Exception try catching the specific Exception type that is thrown by this code. What must you do before calling wait() ? Go through the thread topics a little more and you will understand the reason behind the exception.
The wait() method must be called from synchronized context otherwise a IllegalMonitorStateException might be thrown, when wait() is invoked on an object.
The code in main() is lega,l since Thread-Class implements Runnable-Interface.
The only way to acquire the Monitor of an Objext is to synchronize on it. Otherwise an Exception will be thrown, when wait(), notify() or notifyAll() is invoked on an object from non-synchronized context.
A thread becomes the owner of the object's monitor in one of three ways:
* By executing a synchronized instance method of that object.
* By executing the body of a synchronized statement that synchronizes on the object.
* For objects of type Class, by executing a synchronized static method of that class.
None of this happens in this example so an exceptions will be thrown.