1- Output is: start main thread End main thread aTest bTest 2- Output is: start main thread aTest bTest End main thread 3- One can not be sure what the output will be. 4- Compilation fails. 5- Run-time exception thrown on execution. [ October 29, 2002: Message edited by: Alfred Kemety ]
That's correct, one would jump though to (compile error) since in the back of the mind we have "It must be in a synchronized block" And for your info, it will throw IllegalMonitorStateException 2 times. one for each thread aTest and bTest. How about the following code???
Well, we'll not ask what will the output be, we all know one can't be sure what will be printed, but the question will be: How many IllegalMonitorStateException will be thrown?? and WHY? And if any is thrown, which thread will throw it? Will the other threads continue to execute? Try to THINK and asses the code, NOT copy it and run it... [ October 29, 2002: Message edited by: Alfred Kemety ]
Only when bTest calls method2(). When method2 is called by the thread aTest, the monitor was already acquired.
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Joined: Aug 14, 2002
Right on Shishio, to add: 1- The thread "main" is the thread that throws the Exception at run-time. 2- Even though the main thread is aborted, the other threads aTest and bTest continue execution and print what they should print. A program exits after the last user thread finish executing the run method or abruptly ends for another reason. 3- The output is unpredictable and is dependent on the JVM scheduler.