Bun thread problem

Thread bun thread

Insert picture description here

Create Bun Object

Contains: skin, stuffing, steamed buns Status: true, no false

Code:

public class BaoZi {
String pi;
String xian;
boolean flag=false;
}

Create Bun Producer Process

The producer (baozipu) class is a thread class, which can inherit the Thread
setting thread task run: produce buns
to judge the state of the buns
true: there are buns, the buns
shop calls the wait method to enter the waiting state
false: there is no buns, the
production of buns is
increased Interesting: Alternate production of two kinds of buns (i%2==0) The
buns are produced.
Modify the bun status to true to wake up the food process: Let the food start to eat the buns.
Note:

  • The relationship between Baozipu thread and Baozi thread ------ communication (mutual exclusion)
  • Must be synchronized at the same time to ensure that only one of the two threads is executing
  • The lock object must be unique, you can use the package sub-object as the lock object
  • The Baozipu class and the food class need to pass the Baozi object as the passed parameter .
    1. You need to create a Baozi traversal in the member position
    . 2. Use the parameterized construction method to assign a value to the Baozi variable

Code:

public class BaoZiPu extends Thread {
private BaoZi bz;

public BaoZiPu(BaoZi bz) {
    this.bz = bz;
}

@Override
public void run() {
    int count=0;
     while (true)
    {
        synchronized (bz)
    {
        //对包子状态进行判断
        if(bz.flag==true) {//有包子就等待
            try {
                bz.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //没有包子,被唤醒之后执行,包子铺生产包子,增加一些趣味性,交替生产两种包子
        if(count%2==0)
        {
            bz.pi="薄皮";
            bz.xian="三鲜馅";

        }
        else{
            bz.pi="冰皮";
            bz.xian="牛肉大葱馅";
        }count++;
        System.out.println("包子铺正在生产:"+bz.pi+bz.xian+"包子");
        //生产包子需要等待3秒钟
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //生产完毕后修改包子状态为有
        bz.flag=true;
        //唤醒吃货进程,吃货可以开始吃了
        bz.notify();
        System.out.println("包子铺已经生产好了:"+bz.pi+bz.xian+"包子,吃货可以吃了!");
    }
    }
}
}

Create food process

Consumer class: it is a thread class that can inherit from Thread.
Task (run): eat
buns to judge the state of buns
false no buns
eat goods call wait method to wait for
true buns

After eating steamed buns, change the status of the steamed buns to false, and the steamed buns will wake up the Baozipu thread to produce steamed buns.

Code:

public class ChiHuo extends Thread{
private  BaoZi bz;
public ChiHuo(BaoZi bz) {
    this.bz = bz;
}
@Override
public void run() {
    while (true)
    {
        synchronized (bz){
            if(bz.flag==false) {//包子没有,顾客等待
                try {
                    bz.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }
            System.out.println("吃货正在吃:"+bz.pi+bz.xian+"包子");
            bz.flag=false;
            bz.notify();
            System.out.println("吃货吃完了:"+bz.pi+bz.xian+"包子,包子铺开始生产包子");
            System.out.println("-----------------------------------------------------");
        }
    }
}
}

Create a test class:

void wait()
causes the current thread to wait until another thread calls the notify() method or notifyAll() method of the object.
void notify()
wakes up a single thread that is waiting for the object monitor.
void notifyAll()
wakes up all threads that are waiting for the object monitor.
void wait(long timeout)
causes the current thread to wait until another thread calls the notify() method or the notifyAll() method of the object, or the specified time has passed. Automatically wake up after this time

Code:

public class DemoTest {
public static void main(String[] args) {
    BaoZi bz=new BaoZi();
    //创建包子铺进程,开始生产包子;
    new BaoZiPu(bz).start();
    //创建吃货进程,开启,吃包子
    new ChiHuo(bz).start();
}
}

display effect:

Baozi Shop is producing: Baozi with Sanxian Stuffing,
Baozipu has produced: Baozi with Sanxian Stuffing, and you can eat it!
Foodie is eating: thin-skin three
fresh stuffed steamed buns. Food is finished: thin-skinned three fresh stuffed steamed buns. Baozi shop began to produce steamed buns

Steamed buns are producing: ice-skinned beef and green onion buns. The
buns are already producing: snow-skinned beef and green onion buns, ready to eat!
Foodie is eating: ice-skin beef buns with green onion stuffing Food is
finished: ice-skin beef buns with green onion stuffing, Baozipu started to produce buns

Baozi Shop is producing: Baozi with Sanxian Stuffing,
Baozipu has produced: Baozi with Sanxian Stuffing, and you can eat it!
Foodie is eating: thin-skin three
fresh stuffed steamed buns. Food is finished: thin-skinned three fresh stuffed steamed buns. Baozi shop began to produce steamed buns

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/102631560
Bun
Recommended