syncronized重入的几种方式
1.多个同步方法互相调用
public class synDubbo {
public synchronized method1(){
System.out.print("进入method1方法");
method2();
}
public synchronized method2(){
System.out.print("进入method2方法");
method3();
}
public synchronized method3(){
System.out.print("进入method2方法");
}
public static void main(String[] args){
final synDubbo dubbo = new synDubbo();
Thread t1 = new Thread(new Runnable(){
@Overide
public void run(){
dubb0.method1();
}
})
t1.start();
}
}
2.子类继承父类的synchronized方法
public class syndubbo2{
public int k = 10;
public class Main {
public synchronized void oprationSub(){
try{
System.out.println("Main print i = " + i);
Thread.sleep(1000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
public class Sub extends Main(){
public synchronized void oprationSub(){
try{
if(k>0){
k--;
System.out.println("Main print i = " + i);
Thread.sleep(1000);
this.oprationSub();
}
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}