package com.itheima.demo01.WaitAndNotify;
/*
Resource class: Package subclass Setting properties of buns skin Subsidence Status of bun: true, no false
*/
public class BaoZi {
//skin String pi; //Subsidence String xian; //Status of bun: true, no false, set initial value to false, no bun boolean flag = false;
}
package com.itheima.demo01.WaitAndNotify;
/*
Producer (bun shop) class: is a thread class that inherits Thread Set thread task (run): Produce buns Judging the status of buns true:with buns The bun shop calls the wait method to enter a wait state false: no buns Bun shop to produce buns Add some fun: Alternate production of two kinds of buns There are two states (i%2==0) The bun shop has made buns Modify the status of the bun to true Wake up the feeding thread and let it eat buns Be careful: Package Packing Thread and Package Subthread Relationships --> Communication (Mutual Exclusion) Must synchronize technology to ensure that only one thread can execute Lock object must be unique and can use package object as lock object The bun shop class and the food class need to pass in the bun object as a parameter 1. You need to create a package Subvariable at the member location 2. Assign a value to this Subvariable using a parameterized construction method
*/
public class BaoZiPu extends Thread{
//1. You need to create a package Subvariable at the member location private BaoZi bz; //2. Assign a value to this Subvariable using a parameterized construction method public BaoZiPu(BaoZi bz) { this.bz = bz; } //Set thread task (run): Produce buns @Override public void run() { //Define a variable int count = 0; //Let the bun shop keep making buns while(true){ //Must synchronize technology to ensure that only one thread can execute synchronized (bz){ //Judging the status of buns if(bz.flag==true){ //The bun shop calls the wait method to enter a wait state try { bz.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //Execute after waking up, bun shop produces buns //Add some fun: Alternate production of two kinds of buns if(count%2==0){ //Production of thin-skinned and three-fresh stuffed buns bz.pi = "Thin skin"; bz.xian = "San Fresh Filling"; }else{ //Production of chilled beef and scallions bz.pi = "Ice Skin"; bz.xian = "Beef scallion sauce"; } count++; System.out.println("The bun shop is in production:"+bz.pi+bz.xian+"Steamed stuffed bun"); //It takes 3 seconds to make buns try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //The bun shop has made buns //Modify the status of the bun to true bz.flag = true; //Wake up the feeding thread and let it eat buns bz.notify(); System.out.println("The bun shop is ready to produce:"+bz.pi+bz.xian+"Steamed stuffed bun,Eating is ready to start"); } } }
}
package com.itheima.demo01.WaitAndNotify;
/*
Consumer (eating) class: is a threaded class that inherits Thread Set thread task (run): Eat buns Judging the status of buns false: no buns Eating calls the wait method into a wait state true:with buns Eat food and buns Finish eating buns Modify bun status to false without Wake up the bun shop thread to produce buns
*/
public class ChiHuo extends Thread{
//1. You need to create a package Subvariable at the member location private BaoZi bz; //2. Assign a value to this Subvariable using a parameterized construction method public ChiHuo(BaoZi bz) { this.bz = bz; } //Set thread task (run): Eat buns @Override public void run() { //Use Dead Loop to keep eating buns while (true){ //Must synchronize technology to ensure that only one thread can execute synchronized (bz){ //Judging the status of buns if(bz.flag==false){ //Eating calls the wait method into a wait state try { bz.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //Code to execute after waking up, eat buns System.out.println("Eating:"+bz.pi+bz.xian+"Steamed buns from"); //Finish eating buns //Modify bun status to false without bz.flag = false; //Wake up the bun shop thread to produce buns bz.notify(); System.out.println("The food has been eaten:"+bz.pi+bz.xian+"Finished eating buns,The bun shop started making buns"); System.out.println("----------------------------------------------------"); } } }
}
package com.itheima.demo01.WaitAndNotify;
/*
Test class: Contains the main method, entry to program execution, and startup program Create package object; Create bun shop thread, open and produce buns; Create eating thread, open, eat buns;
*/
public class Demo {
public static void main(String[] args) { //Create package object; BaoZi bz =new BaoZi(); //Create bun shop thread, open and produce buns; new BaoZiPu(bz).start(); //Create eating thread, open, eat buns; new ChiHuo(bz).start(); }
}