In the second chapter of java concurrent programming practice, the concept and function of "reentrant lock" are introduced, and the built-in lock synchronized of java is A reentrant lock. The Widget and LogginWidget are mentioned here. The source code is as follows:
public class Widget { public synchronized void doSomething() { // do somethig here... }}public class LoggingWidget extends Widget { public synchronized void doSomething() { System.out.println(toString() + ": calling doSomething"); super.doSomething(); }}
The description is as follows:
The subclass overwrites the synchonized method of the parent class, and then calls the method in the parent class. If there is no re-entry lock, this code will generate a deadlock. Since the doSomething methods in widgets and loggingwidgets are both synchonized methods, the lock on the Widget is obtained before each doSomething method is executed. HoweverIf the built-in lock is not reentrant, the lock on the Widget cannot be obtained when super. doSomething () is called.Because the lock has been held, the thread will pause forever and wait for a lock that will never be obtained.
You may be confused when you see the font in the red part. Should the lock obtained when the thread enters LoggingWidget. doSomething () be the LoggingWidget object lock? How can it be a lock on a Widget? Which object lock does super. doSomething () obtain? For me who like to investigate details, I have all kinds of questions at this time, especially after I write my first blog: java concurrent programming practice (I) java class lock and object lock.Literally, it seems that when the synchronization method of the subclass object is executed, the lock of the parent object will also be obtained. If the lock is not reentrant, the super will be called again. doSomething () does not succeed if it wants to obtain the Widget object lock for the second time.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1eLA777NsvrJ + release/zo7/release/Msv8 + release/zPzwvcD4KPHA + release/release + release/zyse4 + bG + release/ztcTNrMqxu + release/ release/E2KO/release/VvOSjqLS0vai21M/release/ydLU1eLR + release/release + b7NsvrJ + release/Wu9PQ19PA4LbUz /OjrMO709C4uMDgttTP86GjPC9wPgo8cD48YnI + cda-vcd4kpha + tb3V4sDvo6zO0sPH0rK + fingerprint = "brush: java; "> LoggingWidget widget = new LoggingWidget (); widget. doSomengthing ();
Because doSomething () is a synchronized method, the lock of the widget object is obtained first during execution. in doSomething (), because the methods in the parent class are also synchronized methods, you must first obtain the object lock. Because there is no so-called parent object, or the parent object is a sub-object, you need to obtain the lock of the widget object. In this way, if it is not a reentrant lock, a deadlock will occur.
Next we will prove through examples:The parent class object is a subclass object, that is,The synchronized method and the synchronized Method of the subclass belong to the same object.
package net.aty.lock.extend;public class BaseClass{public synchronized void doSomeThing(){System.out.println("parent class:begin.....doSomeThing");try{Thread.sleep(200);} catch (InterruptedException e){e.printStackTrace();}System.out.println("parent class:end.....doSomeThing");}}
package net.aty.lock.extend;public class ChildClass extends BaseClass{public synchronized void childMethod(){System.out.println("---child class:begin.....childMethod");try{Thread.sleep(200);} catch (InterruptedException e){e.printStackTrace();}System.out.println("---child class:end.....childMethod");}}
package net.aty.lock.extend.thread;import net.aty.lock.extend.BaseClass;public class DemoThread1 extends Thread{private BaseClass base = null;public DemoThread1(BaseClass base){this.base = base;}@Overridepublic void run(){base.doSomeThing();}}
package net.aty.lock.extend.thread;import net.aty.lock.extend.ChildClass;public class DemoThread2 extends Thread{private ChildClass child = null;public DemoThread2(ChildClass child){this.child = child;}@Overridepublic void run(){child.childMethod();}}
My test idea is: let a thread access the synchronized Method in the parent class, and then let another thread access the synchronized Method of the subclass. Because the two threads compete for the lock of the same object, thread 1 will not start execution if it is not finished.
package net.aty.lock.extend;public class ChildClass extends BaseClass{public synchronized void childMethod(){System.out.println("---child class:begin.....childMethod");try{Thread.sleep(200);} catch (InterruptedException e){e.printStackTrace();}System.out.println("---child class:end.....childMethod");}}
The execution result is:
Parent class: begin... doSomeThing
... Main running...
... Main running...
Parent class: end... doSomeThing
--- Child class: begin... childMethod
--- Child class: end... childMethod
Obviously, the program meets our expectation and does not have a parent object. There is no substantial difference between the synchronization method for accessing the parent class and the synchronization method for accessing the Child class, to obtain the lock of the subclass object.