java - reason of IllegalMonitorStateException if I use wait within sync block -
I try to understand Java core synchronization.
I wrote a code sample:
write program
left right
10 times
package concurrency; Public square LeftRightWaitNotifyExample {last fixed string str = "1"; Public static zero main (string [] Args) {New LeftLegThread (str). Start (); New RightLegThread (str) Start (); }} Class LeftLegThread thread {String Monitor} extends; Public LeftLegThread (string str) {monitor = str; } @ Override Public Zero Run () {try} {makeStep (); } Grip (Interrupted ejection) {// TODO Auto-Generated Catch Block e.printStackTrace (); }} Private Zero makeStep () throws interruptedException {synchronize (monitors) {for (int i = 0; i & lt; 10; i ++) {println ("left"); stop(); }}}} Class extends the RightLegThread thread {String Monitor}; Public RightLegThread (string str) {monitor = str; } @ Override Public Zero Run () {try} {makeStep (); } Grip (Inhibited Aspen E) {}} Private Weed Mastep (Interrupted Interrupted Expressions) {Synchronize} {while (true) {System.out.println ("right"); Notify(); stop(); }}}}
I get this output:
left-right thread "thread-0" java.lang at java.lang.IllegalMonitorStateException . Object.wait (native resident method) java.lang.Object.wait (object.java:485) concurrency.LeftLegThread.makeStep (LeftRightWaitNotifyExample.java35) at concurrency.LeftLegThread.run at (LeftRightWaitNotifyExample.java:23) exception At the Concurrency Right-Thread- / Code>
When I did not use the
wait method under the
synchronized block, before that That I found this error. But here I use waiting within the
synchronize block
, what is the cause of the problem and how it is decided?
Update
I rewrite the code as advised:
public class LeftRightWaitNotifyExample {last fixed string str = "1"; Public static zero principal (string [] ARGs throws interrupted expression (new left legTrade (str). Start (); Thread.Sleep (100); New RightLegThread (str) Start (); }} Class LeftLegThread thread {String Monitor} extends; Public LeftLegThread (string str) {monitor = str; } @ Override Public Zero Run () {try} {makeStep (); } Grip (Interrupted ejection) {// TODO Auto-Generated Catch Block e.printStackTrace (); }} Private Zero makeStep throws interruptedException {synchronize (for {{int i = 0; i & lt; 2; i ++) {println ("left"); Monitor.wait (); Monitor.notify (); }}}} Class extends the RightLegThread thread {String Monitor}; Public RightLegThread (string str) {monitor = str; } @ Override Public Zero Run () {try} {makeStep (); } Grip (Inhibited Aspen E) {}} Private Weed Mastep (Interrupted Interrupted Expressions) {Synchronize} {while (true) {System.out.println ("right"); Monitor.notify (); Monitor.wait (); Current} <>
current output:
left right alright right
why
right < / Code> Boycott 3 But
Left Why only two times?
You are syncing on
monitor , so you
wait () on the monitor, also:
monitor. Wait ();
Right now you are waiting on
this , which is not the monitor owner, because there is a synchronization
monitor . >
Note that definitely
notify should also be done on the
monitor object and that you
notify >> > threads otherwise it may be that a thread is waiting for a missing notification. Using a timeout (overloaded version of
wait ) can also be a good idea to catch corner cases.
Comments
Post a Comment