fred rosenberger wrote:
And we may be having a slight language issues...to me, "run it" means doing this:
java ZiggyTest2
The phrase 'compile it' means this
javac ZiggyTest2.java
Apologies for the language confusion. English is not my first language so i do apologies if my choice of words confused you. What i meant to say was that i compiled and run the program and there was no compilation failure or any runtime exception.
Here is a quote i read from the K&B book. Page 747, chapter 9 Threads (See under the "Thread interaction" heading).
One key point to remember (and keep in mind for the exam) about wait/notify is this:
wait(), notify() and notifyAll() must be called from within a synchronized context! A thread can't invoke a wait or notify method on an object unless it owns that object's lock.
Based on above i decided to write that small program to try and see what happens if wait() was called from a non-synchronised context.
- I was expecting it to either fail or to generate a runtime exception but none of the two happened.
To make things even more confusing: Have a look at this example
Running the above example generates an IllegalMonitorStateException. Why is the above code generating an IllegalMonitorStateException but the previous code does not?
Also if i modify the above code such that the wait() method is not enclosed inside a try/catch block i get a compiler error saying that InterruptedException must be declared or thrown. Again why did i not get this compilation failure on the original code example?