0
    synchronized (lock) {
        //lock.notifyAll();
        new Thread(() -> {
            System.out.println("同步代码块中创建线程...");
            try {
                lock.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }

In this code : throw exception "IllegalMonitorStateException.

  • 1
    Because the started thread is not the owner of the lock. – luk2302 Apr 26 '23 at 07:53
  • [The documentation of the wait method](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Object.html#wait()) is pretty clear about this. – VGR Apr 26 '23 at 13:44

0 Answers0