Hi everyone!
Would any kind soul out there please be able to help me out with a problem with threading? Am being driven demented!
My code instantiates 3 instances of the same class and increments a shared variable. I want each instance to go to sleep until the variable has reached a certain value through being incremented by other instances of the class. So I thought I could use wait and notifyAll.. here's the code
When I run this code, it throws an IllegalMonitorStateException at the x.notifyAll() line:
Oi 1 Added 1, now x = 1
Whoops id = 1, Illegl java.lang.IllegalMonitorStateException: current
thread not
owner
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at Oi.run(test.java:29)
at java.lang.Thread.run(Thread.java:534)
Oi 1 WAITING cos x = 1
Oi 2 Added 1, now x = 2
Whoops id = 2, Illegl java.lang.IllegalMonitorStateException: current thread not
owner
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at Oi.run(test.java:29)
at java.lang.Thread.run(Thread.java:534)
Oi 2 WAITING cos x = 2
java.lang.IllegalMonitorStateException: current thread not owner
Oi 3 Added 1, now x = 3
Whoops id = 3, Illegl java.lang.IllegalMonitorStateException: current thread not
owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:429)
at Oi.run(test.java:40)
at java.lang.Thread.run(Thread.java:534)
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notifyAll(Native Method)
at Oi.run(test.java:29)
at java.lang.Thread.run(Thread.java:534)
yay! Oi 3 finished waiting
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:429)
at Oi.run(test.java:40)
at java.lang.Thread.run(Thread.java:534)
I don't understand why the call to notifyAll throws this exception. Its within the synchronized block of code, so surely to enter into this block, the thread MUST get ownership of x's monitor? Or am I barking up the wrong tree completely?
I would be eternally grateful to any of ye gurus out there who can cast some light on this for me - its driving me bonkers
Many thanks!
cheers guys
binkie
[ EJFH: Fixed code tags. Use "[]", not "<>" ].
[ August 04, 2004: Message edited by: Ernest Friedman-Hill ]