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....objB......");
synchronized(MyLock.objA){
System.out.println("false....objA.....");
}
}
}
}
}
class ThreadDemo {
public static void main(String[] args) {
// 创建线程对象
MyThread t1 = new MyThread(true) ;
MyThread t2 = new MyThread(false) ;
// 启动线程
t1.start() ;
t2.start() ;
}
}
class MyLock {
public static final Object objA = new Object() ;
public static final Object objB = new Object() ;
} |
|