八 死锁
同步中嵌套死锁
public class DeadLockDemo {
public static void main(String[] args) {
Thread t1 = new Thread(new Testt(true));
Thread t2 = new Thread(new Testt(false));
t1.start();
t2.start();
}
}
class Testt implements Runnable{
private boolean flag;
Testt(boolean flag){
this.flag = flag;
}
public void run() {
if(flag){
while(true){
synchronized (myLock.locka) {
System.out.println("if--locka");
synchronized (myLock.lockb) {
System.out.println("if--lockb");
}
}
}
}
else{
while(true){
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();
}
|
|