同步方法
public class ReEnterLockDemo {
static Object objectLockA = new Object();
public static void m1(){
new Thread(() -> {
synchronized (objectLockA){
System.out.println(Thread.currentThread().getName()+"\t"+"--------外层调用");
synchronized (objectLockA){
System.out.println(Thread.currentThread().getName()+"\t"+"--------中层调用");
synchronized (objectLockA){
System.out.println(Thread.currentThread().getName()+"\t"+"--------内层调用");
}
}
}
},"t1").start();
}
public static void main(String[] args) {
m1();
}
}
同步代码快
public class ReEnterLockDemo {
public synchronized void m1(){
System.out.println("=========外层函数");
m2();
}
public synchronized void m2(){
System.out.println("=========中层函数");
m3();
}
public synchronized void m3(){
System.out.println("=========内层函数");
}
public static void main(String[] args) {
new ReEnterLockDemo().m1();
}
}