Object class wait with parameter method and notify

Posted by Static_Nexus on Tue, 30 Jul 2019 18:08:22 +0200

package com.itheima.demo10.WaitAndNotify;
/*

There are two ways to get into TimeWaiting
 1. Using the sleep(long m) method, the thread wakes up to the Runnable/Blocked state after the end of the millisecond value
 2. Using the wait(long m) method, the wait method will automatically wake up if it has not been awakened by notify after the end of the millisecond value, and the thread will wake up to the Runnable/Blocked state

Ways to wake up:
     void notify() wakes up a single thread waiting on this object monitor.
     void notifyAll() wakes up all threads waiting on this object monitor.

*/
public class Demo02WaitAndNotify {

public static void main(String[] args) {
    //Create lock objects to ensure uniqueness
    Object obj = new Object();
    // Create a customer thread (consumer)
    new Thread(){
        @Override
        public void run() {
            //Waiting to buy buns
            while(true){
                //Ensuring that only one thread can be executed while waiting and waking up requires synchronization technology
                synchronized (obj){
                    System.out.println("Customer 1 tells the boss what kind and quantity of buns he wants");
                    //Call the wait method, discard the execution of the cpu, and enter the WAITING state (wait indefinitely)
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //Code executed after wakeup
                    System.out.println("The buns are ready,Customer 1 eats!");
                    System.out.println("---------------------------------------");
                }
            }
        }
    }.start();

    // Create a customer thread (consumer)
    new Thread(){
        @Override
        public void run() {
            //Waiting to buy buns
            while(true){
                //Ensuring that only one thread can be executed while waiting and waking up requires synchronization technology
                synchronized (obj){
                    System.out.println("Customer 2 tells the boss what kind and quantity of buns he wants");
                    //Call the wait method, discard the execution of the cpu, and enter the WAITING state (wait indefinitely)
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //Code executed after wakeup
                    System.out.println("The buns are ready,Customer 2 Serves!");
                    System.out.println("---------------------------------------");
                }
            }
        }
    }.start();

    //Create a Boss Thread (Producer)
    new Thread(){
        @Override
        public void run() {
            //Keep making buns
            while (true){
                //It took 5 seconds to make buns
                try {
                    Thread.sleep(5000);//Make buns in 5 seconds
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //Ensuring that only one thread can be executed while waiting and waking up requires synchronization technology
                synchronized (obj){
                    System.out.println("The boss makes the buns in five seconds,Inform Customer,You can eat buns now");
                    //Once the buns are ready, call the notify method to wake up the customer to eat the buns
                    //obj.notify(); //If there are multiple waiting threads, wake up one at random
                    obj.notifyAll();//Wake up all waiting threads
                }
            }
        }
    }.start();
}

}

Topics: Java