死锁代码:
public class DeadLockThread extends Thread {
boolean flag;//定义标记,用来指定要执行的代码
public DeadLockThread(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
if(flag) {//flag赋值为true时,执行的代码
synchronized (锁1) {
System.out.println(“if中锁1");
synchronized (锁2) {
System.out.println(“if中锁2");
}
}
} else {//flag赋值为false时,执行的代码
synchronized (锁2) {
System.out.println(“else中锁2");
synchronized (锁1) {
System.out.println(“else中锁1");
}
}
}
}
}
|
|