package cn.itheima.dielock01;
public class DieLockDemo01 {
public static void main(String[] args) {
MyThread mt1 = new MyThread(true);
MyThread mt2 = new MyThread(false);
mt1.start();
mt2.start();
}
}
package cn.itheima.dielock01;
public class MyLock {
public static final Object objA = new Object();
public static final Object objB = new Object();
}
package cn.itheima.dielock01;
public class MyThread extends Thread{
private boolean flag;
public MyThread(boolean flag) {
this.flag = flag;
}
public void run(){
if(flag){
synchronized(MyLock.objA){
System.out.println("true --- objA");
synchronized(MyLock.objB){
System.out.println("true --- objB");
}
}
}
else{
synchronized(MyLock.objB){
System.out.println("false --- objA");
synchronized(MyLock.objA){
System.out.println("false --- objB");
}
}
}
}
}
|