Java multi-threading - the use of wait / notify to communicate between threads

wait () method

Method of action wait () is to code currently executing thread to wait, wait () method is a method Object class, which is used to thread into the current "pre-execution queue", the code and the () resides in the wait line stops execution until notified or interrupted so far.

note:

  Before calling wait (), thread must obtain a lock object level of the object, otherwise it will throw an exception illegalMonitorStateException. After performing the wait () method, the current thread releases the lock.

 

notify () method

The method notify () method should be in sync or sync block calls, that before calling thread must obtain a lock object level of the object, otherwise it will throw an exception illegalMonitorStateException.

The method used to notify other threads that may be waiting for this object's lock, if there are multiple threads waiting, by the thread planner randomly selected at a time was always a wait state thread, its notification notify.

note:

  After 1) the implementation of notify (), the current thread will not immediately release the object lock, the thread in a wait state also does not get immediately the object lock, until the implementation of notify () method of the thread executing the program, which is synchronized to exit after the block, the current thread will release the lock, and the thread in a wait state where it can acquire the object lock.

  2) When the object is to get a lock wait thread has finished running, it will release the object lock, at this time if the object does not use notify statement again, even if the object has been idle, waiting for the other thread wait state because there is no notice, It will continue blocking the wait state

 

 

Examples

public class Test {
    public static void main(String[] args) {
        try {
            Object lock = new Object();
            MyThread1 t1 = new MyThread1(lock);
            t1.start();
            Thread.sleep(2000);
            MyThread2 t2 = new MyThread2(lock);
            t2.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class MyThread1 extends Thread {
        private Object lock;

        public MyThread1(Object lock) {
            this.lock = lock;
        }

        @Override
        public void run() {
            try {
                synchronized (lock) {
                    System.out.println("开始 wait");
                    lock.wait();
                    System.out.println("结束 wait");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class MyThread2 extends Thread {
        private Object lock;

        public MyThread2(Object lock) {
            this.lock = lock;
        }

        @Override
        public void run() {
            synchronized (lock) {
                System.out.println("开始 notify");
                lock.notify();
                System.out.println("结束 notify");
            }
        }
    }
}

operation result:

 

Guess you like

Origin www.cnblogs.com/lkc9/p/12514625.html