While studying for OJP I came to the topic of Thread wait() method and so on, According to the book, this portion of code should throw IllegalMonitorStateException since the wait() is invoked on an object other than the one synchronized on, but I doesn't with me, Any Idea?
public class Syncho {
public void synchoTest(){
ThreadTest test1 = new ThreadTest();
ThreadTest test2 = new ThreadTest();
ThreadTest test3 = new ThreadTest();
ThreadTest test4 = new ThreadTest();
ThreadTest test5 = new ThreadTest();
test1.start();
test2.start();
synchronized(test1){
try{
System.out.println("gere");
//test1 = new ThreadTest();
//test1.start();
wait();
System.out.println("tere");
}catch(Exception x){}
}
//notify();
test3.start();
test4.start();
test5.start();
}
}
actually "gere" only is printed not "tere"
I am using eclipse with java 7
Yes, an exception is being thrown, for exactly the reason you describe - you're just catching it and swallowing it:
catch(Exception x){}
Don't do that. Never do that. Just doing this:
catch (Exception x) {
x.printStackTrace();
}
... will show you the exception being thrown.
See more on this question at Stackoverflow