wait/notify/notifyall

wait和notify 等待和唤醒 , 需要包围在synchronized里,当获取到同一把锁,notify就能唤醒wait

 

1. wait和notify使用:

当只调用wait,不调用notify,

public class TestWaitNotify {
    public static void main(String[] args) {

        Object object = new Object();
        Thread t1 = new Thread(() -> doWait(object));
        t1.start();

//        Thread t2 = new Thread(() -> doNotify(object));
//        t2.start();
    }

    public static void doWait(Object object) {
        synchronized (object) {
            try {
                System.out.println("wait....");
                object.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void doNotify(Object object) {
        synchronized (object) {
            System.out.println("notify...");
            object.notify();
        }
    }
}

 会一直等待状态

 

当调用了wait,又用同一个锁(object )调用notify, 

public class TestWaitNotify {
    public static void main(String[] args) {

        Object object = new Object();
        Thread t1 = new Thread(() -> doWait(object));
        t1.start();

        Thread t2 = new Thread(() -> doNotify(object));
        t2.start();
    }

    public static void doWait(Object object) {
        synchronized (object) {
            try {
                System.out.println("wait....");
                object.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void doNotify(Object object) {
        synchronized (object) {
            System.out.println("notify...");
            object.notify();
        }
    }
}

 就能执行完并且释放锁。

 

 

2. notify和notifyall

看下面一个例子:

public class TestWaitNotify {
    public static void main(String[] args) {

        Object object = new Object();
        for (int i = 0; i < 5; i++) {
            Thread t1 = new Thread(() -> doWait(object));
            t1.start();
        }
        
        Thread t2 = new Thread(() -> doNotify(object));
        t2.start();
    }

    public static void doWait(Object object) {
        synchronized (object) {
            try {
                System.out.println("wait....");
                object.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void doNotify(Object object) {
        synchronized (object) {
            System.out.println("notify...");
            object.notify();
        }
    }
}

 启动了五个线程,但是notify只会唤醒一个,其余的都还在等待。

public class TestWaitNotify {
    public static void main(String[] args) {

        Object object = new Object();
        for (int i = 0; i < 5; i++) {
            Thread t1 = new Thread(() -> doWait(object));
            t1.start();
        }

        Thread t2 = new Thread(() -> doNotifyAll(object));
        t2.start();
    }

    public static void doWait(Object object) {
        synchronized (object) {
            try {
                System.out.println("wait....");
                object.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void doNotify(Object object) {
        synchronized (object) {
            System.out.println("notify...");
            object.notify();
        }
    }

    public static void doNotifyAll(Object object) {
        synchronized (object) {
            System.out.println("notify all...");
            object.notifyAll();
        }
    }
}

 使用notifyall,会一次性唤醒所有线程。

 

posted @ 2021-06-17 04:11  圣金巫灵  阅读(48)  评论(0编辑  收藏  举报