public class Demo06 {
public static void main(String[] args) {
Thread t1 = new Thread(new Test02(true));
Thread t2 = new Thread(new Test02(false));
t1.start();
t2.start();
}
}
class Test02 implements Runnable{
private boolean falg ;
Test02(boolean falg){
this.falg = falg;
}
public void run(){
if(falg){
while(true){
synchronized(MyLock.locka){
System.out.println("if locka");
synchronized(MyLock.lockb){
System.out.println("if lockb");
}
}
// falg = false;
}
}else{
while(true){
synchronized(MyLock.locka){
System.out.println("else locka");
synchronized(MyLock.lockb){
System.out.println("else lockb");
}
}
// falg = true;
}
}
}
}
class MyLock{
static Object locka = new Object();
static Object lockb = new Object();
}
|
|