このコードがデッドロックに陥っている理由を誰か教えてください。
public class BrokenOrderingReentredLock implements Runnable {
private Object lock1 = new Object();
private Object lock2 = new Object();
public static void main(String[] args) throws InterruptedException {
BrokenOrderingReentredLock runnable = new BrokenOrderingReentredLock();
Thread thread1 = new Thread(runnable, "thread1");
Thread thread2 = new Thread(runnable, "thread2");
thread1.start();
Thread.sleep(500);
thread2.start();
}
@Override
public void run() {
try {
String threadName = Thread.currentThread().getName();
synchronized (lock1) {
System.out.println(threadName + " has lock1");
synchronized (lock2) {
System.out.println(threadName + " has lock2");
System.out.println(threadName + " reenters lock1");
lock1.wait();
lock2.wait();
}
}
} catch (Exception e) {
e.printStackTrace();
}
} }