java线程死锁

package cn.star;
//死锁

class Test implements Runnable{
private boolean flag;

Test(boolean flag) {
this.flag = flag;

}

public void run() {
if (flag) {
synchronized (MyLock.locka) {
System.out.println("if_locka");
synchronized (MyLock.lockb) {
System.out.println("if_lockb");
}

}
} else

{
synchronized (MyLock.lockb) {
System.out.println("else_lockb");
synchronized (MyLock.locka) {
System.out.println("else_locka");

}

}
}

}
}

class MyLock {
static Object locka = new Object();
static Object lockb = new Object();
}

 class Demoi {
public static void main(String[] args) {
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}

}

评论

热门博文