线程wait和notify基础

2019-04-13  本文已影响0人  陈柴盐

public class Lock {
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();

    new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (obj) {
                try {
                    System.out.println("等待前");
                    obj.wait(); // obj
                    System.out.println("等待后");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (obj) {
                    System.out.println("唤醒前");
                    obj.notify();
                    System.out.println("唤醒后");
            }
        }
    }).start();
}

}

上一篇 下一篇

猜你喜欢

热点阅读