Waiting for wake-up case code

Posted by ckk on Tue, 30 Jul 2019 04:28:53 +0200


package com.itheima.demo10.WaitAndNotify;
/*

Waiting for wake-up cases: communication between threads
    Create a customer thread (consumer): inform the owner of the type and number of buns you want, call the wait method, abandon the execution of the cpu, and enter the WAITING state (unlimited waiting)
    Create a Boss Thread (Producer): Take 5 seconds to make steamed buns. After making the steamed buns, call notify method to wake up customers to eat steamed buns.

Be careful:
    Customer and owner threads must be wrapped in synchronous code blocks to ensure that only one wait and wake-up can be executed
    The lock object used synchronously must be guaranteed uniqueness
    Only lock objects can call wait and notify methods

Method in Obejct class
void wait()
      Causes the current thread to wait before other threads call the notify() method or notifyAll() method of this object.
void notify()
      Wake up a single thread waiting on this object monitor.
      The code after the wait method continues to execute

*/
public class Demo01WaitAndNotify {

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 steamed buns
           while(true){
               //Ensuring that there is only one thread to wait and wake up requires synchronization
               synchronized (obj){
                   System.out.println("Tell your boss the type and quantity of steamed buns you want");
                   //Call the wait method, abandon the execution of the cpu, and enter the WAITING state (unlimited waiting)
                   try {
                       obj.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   //Code executed after wake-up
                   System.out.println("The steamed buns are ready.,Open to eat!");
                   System.out.println("---------------------------------------");
               }
           }
        }
    }.start();

    //Create a Boss Thread (Producer)
    new Thread(){
        @Override
        public void run() {
            //Make steamed buns all the time
            while (true){
                //It took five seconds to make steamed buns.
                try {
                    Thread.sleep(5000);//Take 5 seconds to make steamed buns
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //Ensuring that there is only one thread to wait and wake up requires synchronization
                synchronized (obj){
                    System.out.println("The boss makes the steamed buns in five seconds,Inform customers,You can eat steamed buns.");
                    //After making the steamed bun, call notify method to wake up the customer to eat the steamed bun.
                    obj.notify();
                }
            }
        }
    }.start();
}

}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.itheima.demo10.WaitAndNotify;
/*

There are two ways to get into Time Waiting
 1. Using sleep(long m) method, the thread wakes up to Runnable/Blocked state after the end of the millisecond value.
2. Using the wait(long m) method, if the wait method is not awakened by notify after the end of the millisecond value, it will wake up automatically, and the thread will wake up into 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 steamed buns
            while(true){
                //Ensuring that there is only one thread to wait and wake up requires synchronization
                synchronized (obj){
                    System.out.println("Customer 1 informs the owner of the type and quantity of steamed buns he wants.");
                    //Call the wait method, abandon the execution of the cpu, and enter the WAITING state (unlimited waiting)
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //Code executed after wake-up
                    System.out.println("The steamed buns are ready.,Customer 1 eats!");
                    System.out.println("---------------------------------------");
                }
            }
        }
    }.start();

    // Create a customer thread (consumer)
    new Thread(){
        @Override
        public void run() {
            //Waiting to buy steamed buns
            while(true){
                //Ensuring that there is only one thread to wait and wake up requires synchronization
                synchronized (obj){
                    System.out.println("Customer 2 informs the owner of the type and quantity of steamed buns he wants.");
                    //Call the wait method, abandon the execution of the cpu, and enter the WAITING state (unlimited waiting)
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //Code executed after wake-up
                    System.out.println("The steamed buns are ready.,Customer 2 eats!");
                    System.out.println("---------------------------------------");
                }
            }
        }
    }.start();

    //Create a Boss Thread (Producer)
    new Thread(){
        @Override
        public void run() {
            //Make steamed buns all the time
            while (true){
                //It took five seconds to make steamed buns.
                try {
                    Thread.sleep(5000);//Take 5 seconds to make steamed buns
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //Ensuring that there is only one thread to wait and wake up requires synchronization
                synchronized (obj){
                    System.out.println("The boss makes the steamed buns in five seconds,Inform customers,You can eat steamed buns.");
                    //After making the steamed bun, call notify method to wake up the customer to eat the steamed bun.
                    //obj.notify(); / / / If there are multiple waiting threads, wake up one at random
                    obj.notifyAll();//Wake up all waiting threads
                }
            }
        }
    }.start();
}

}

Topics: Java