为什么以下代码有死锁?

use*_*631 2 java multithreading deadlock

谁能告诉我为什么这段代码会遇到死锁?

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();
        }
    } }
Run Code Online (Sandbox Code Playgroud)

Sot*_*lis 7

Thread1开始并获得lock1lock2.Thread1发布lock1(带lock1.wait()).

Thread2开始lock1然后等待lock2永远.Thread1等待通知,但永远不会.

僵局!