package com.pinnet.test;

public class Demo {

    public static void main(String[] args) {

        Demo demo = new Demo();

        new Thread(new Runnable() {

            @Override
            public void run() {
                synchronized (demo) {
                    System.out.println("before wait ");
                    try {
                        demo.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("after wait ");
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                synchronized (demo) {
                    System.out.println("before notify ");
                    demo.notify();
                    System.out.println("after notify ");
                }
            }
        }).start();

    }
}

调用notify()后,当前线程执行完synchronized块中的所有代码才会释放锁

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
  • 2021-06-25
猜你喜欢
  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2021-10-15
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案