public class DeadLock implements Runnable {
Object locka=new Object();
Object lockb=new Object();
@Override
public void run() {
while(true){
synchronized (locka) {
System.out.println("11locka");
synchronized (lockb) {
System.out.println("11lockb");
}
}
synchronized (lockb) {
System.out.println("22lockb");
synchronized (locka) {
System.out.println("22locka");
}
}
}
}
}
public class Test {
public static void main(String[] args) {
DeadLock dk=new DeadLock();
Thread t1=new Thread(dk,"aa");
Thread t2=new Thread(dk,"bb");
t1.start();
t2.start();
}
}
这样也可以死锁,但是在while里面线程为什么可以直接去走第二个锁 |