Anayonkar Shivalkar wrote:The code will compile, but will throw a java.lang.IllegalMonitorStateException.
It throws an IllegalMonitorStateException simply because thread "t" calling the wait() method doesn't own the lock with a synchronize(t){...} code block.
In reference to Kathy & Bert book on page 787: Thread Interaction (Exam Objective 4.4) 749 750 Chapter 9: Threads
Note that if the thread calling wait() does not own the lock, it will throw an
IllegalMonitorStateException. This exception is not a checked exception,
so you don't have to catch it explicitly.
You should always be clear whether a thread
has the lock of an object in any given block of code.
Reread the Thread chapter and do some more examples that will help reinforce your knowledge and enhance your skills.
Hope it help...