I've seen Guangong idle talk about an instance in a thread, let the main thread run 10 times, and then the child thread runs 20 times, so it loops 50 times. Today, I wrote a bit of idle, just write the code reported a java.lang.IllegalMonitorStateException error, on-line check a bit, found the problem is the lock problem, so I summed up a bit.
First of all, the most important: the basis of syschronized synchronization must be that multiple threads have the same lock (object lock, variable lock), cannot have no lock permission, and cannot be a permission to have a different lock. See the example below.
First version of the error code is posted first:
public class Threadnotity {/** * * @param args * * @deprecated thread 1 executes 10 times, thread 2 executes 20 times, loops 50 times */public static void main (string[ ] args) {//TODO auto-generated method stubfinal A = new A (); New Thread (New Runnable () {@Overridepublic void Run () {//T ODO auto-generated method stubfor (int i = 0; i <; i++) {A.printa ();}}). Start (); New Thread (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfor (int i = 0; i < 50 ; i++) {A.PRINTB ();}}). Start ();}} Class A {Private Boolean isarun = True;private Static Object lock = new Object ();p ublic synchronized void PrintA () {while (!isarun) {try {a.class.wait ();//lock.wait ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int i = 0; i < i++) System.out.println (Thread.CurrentThread (). GetName () + "i=" + i); isarun = false; A.class.notifyall ();//lock.notifyall ();} Public synchronized void Printb () {while (Isarun) {try {a.class.wait (); Lock.wait ();} catch (InterruptedexcEption e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int i = 0; i <; i++) System.out.println (Thread.CurrentThread (). GetName () + "i=" + i); isarun = true; A.class.notifyall (); Lock.notifyall ();}}
This program doesn't work with a.class.wait () or lock.wait () will be reported java.lang.IllegalMonitorStateException error, Baidu Java.lang.IllegalMonitorStateException API explained as: The exception thrown indicates that a thread has tried to wait on the object's monitor, or has attempted to notify other monitors that are waiting on the object and that it does not already have a monitor specified. It can be understood that the running thread does not get the specified object lock (specifically, Class A and object lock in the above program).
So according to synchronized general definition method, and wrote several kinds of solutions.
1.synchronized modification method,
The program can run successfully without adding objects, directly calling the Wait (), notity () method; try to use This,this.wait (), this.notity (), the program runs successfully. And then just think about what this is really referring to. Obviously, it is not a.class and lock. We know that this is usually a pointer to the object calling this method, in this program, the object that invokes the method is a, then a as a method of the parameters, passed in, and selectSynchronized Object Lock is a, call a.wait (), A.notityall (), the program can run successfully, and then mix A and this, still can run successfully. Obviously, when using synchronized to modify the method, the objectlock obtained by synchronized is the object lock of the object calling this method, a, which can also be used here instead.
Class A {Private Boolean isarun = true; private static Object lock = new Object (); <span style= "Font-family:monospace; font-size:13px; line-height:19.2000007629395px; White-space:pre; " >synchronized Modify method </span> public synchronized void PrintA (a a) {while (!isarun) {try {wait ();//this.wait ();// A.wait ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int i = 0; i < i++) System.out.println (Thread.CurrentThread (). GetName () + "i=" + i); Isarun = False;notifyall ( );//this.notifyall ();//a.notifyall ();} Public synchronized void Printb (A-a) {while (Isarun) {try {wait ();//this.wait ();//a.wait ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int i = 0; i <; i++) System.out.println (Thread.CurrentThread (). GetName () + "i=" + i); Isarun = True;notifyall () ;//this.notifyall ();//a.notifyall ();}}
2. synchronized to retouch code snippets
When synchronized modifies a code fragment, the lock can be specified by itself, either as an object lock or as a variable, or as a byte-code A.class of a class. But must be the same.
Class A {Private Boolean isarun = True;private Static Object lock = new Object ();p ublic void PrintA (a a) {synchronized (lock) {while (!isarun) {try {//a.class.wait (); lock.wait ()} catch (Interruptedexception e) {//TODO auto-generated catch Blocke . Printstacktrace ();}} for (int i = 0; i < i++) System.out.println (Thread.CurrentThread (). GetName () + " i=" + i); isarun = false; lock.no Tifyall (); A.class.notifyall ();}} Public void Printb (A-a) {synchronized (lock) {while (Isarun) {try {//a.class.wait (); lock.wait ();} catch (Interrupted Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} for (int i = 0; i <; i++) System.out.println (Thread.CurrentThread (). GetName () + " i=" + i); isarun = true; lock.not Ifyall (); A.class.notifyall (); }}}
syschronized lock problem in multi-threaded synchronization