锁的重入
package demo;
public class MyDubbo {
public synchronized void method1(){
System.out.println("method1....");
method2();
}
public synchronized void method2(){
System.out.println("method2....");
method3();
}
public synchronized void method3(){
System.out.println("method3....");
}
public static void main(String[] args) {
final MyDubbo my1 = new MyDubbo();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
my1.method1();
}
});
t1.start();
}
}
子父类的重入
package demo;
public class MyDubbo2 {
static class Main{
public int num = 10 ;
public synchronized void operationMainSup(){
try {
num--;
System.out.println("Main print num = " + num );
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class Sub extends Main{
public synchronized void operationSubSup() {
try {
while(num>0){
num--;
System.out.println("Sub print num = " + num );
Thread.sleep(100);
this.operationMainSup();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
Sub sub = new Sub();
sub.operationSubSup();
}
});
t1.start();
}
}