JAVA basics--multithreading and thread safety case waiting and wakeup (producer and consumer)

Waiting and Wakeup Cases

Synthetically use thread condition objects and synchronized code blocks to achieve thread safety.
Take the sale of steamed buns as an example. When customers buy steamed buns, they eat them directly. If there are no steamed buns, they need to wait for steamed buns to be made. If there are customers in the steamed bun shop who need steamed buns and there are no steamed buns in the steamed bun store, they need to make steamed buns.
insert image description here
Obviously there are customer threads and Baozipu threads, and the synchronization control of the two threads is realized through the synchronization code block. Baozi class, the get and set methods
of setting the properties of Baozi have been omitted
BaoZi.java

public class BaoZi {
    
    
    private String pi;
    private String xian;
    private Boolean state;

    public BaoZi(String pi, String xian, Boolean state) {
    
    
        this.pi = pi;
        this.xian = xian;
        this.state = state;
    }

    public BaoZi() {
    
    
    }
}

Steamed bun shop, producing steamed stuffed buns

public class BaoZiPu extends Thread{
    
    
    private BaoZi bz;
    private int count=0;

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

    @Override
    public void run() {
    
    
        // 一直生产包子
        while(true) {
    
    
            // 利用同步代码块来实现和吃货线程间的同步
            synchronized (bz) {
    
    
                // 如果有包子,进入等待状态
                if (bz.getState()) {
    
    
                    try {
    
    
                        bz.wait();
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                } else {
    
    
                    // 没包子,生产包子
                    // 生产两种口味的包子
                    if(count%2==0) {
    
    
                        // 生产大葱韭菜陷包子
                        bz.setPi("薄皮");
                        bz.setXian("大葱韭菜");
                    } else {
    
    
                        // 生产腰子卤肉陷包子
                        bz.setPi("厚皮");
                        bz.setXian("腰子卤肉");
                    }
                    System.out.println("顾客,您的"+bz.getPi()+bz.getXian()+"包子正在制作");
                    try {
    
    
                        sleep(3000);
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                    System.out.println("您的"+bz.getPi()+bz.getXian()+"包子已经做好");
                    count++;
                    // 将包子状态设置为true
                    bz.setState(true);
                    // 唤醒吃货线程吃包子
                    bz.notifyAll();
                }
            }
        }
    }
}

Customers, eating buns

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.getState()) {
    
    
                    // 有包子,开吃
                    System.out.println("开始吃包子");
                    bz.setState(false);
                    bz.notifyAll();
                } else {
    
    
                    // 没包子,等待状态
                    try {
    
    
                        bz.wait();
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

Test class, open thread

public class DemoTest {
    
    

    public static void main(String[] args) {
    
    
        BaoZi baoZi = new BaoZi("第一个皮","第一个陷",true);
        BaoZiPu bzp = new BaoZiPu(baoZi);
        bzp.start();
        ChiHuo ch = new ChiHuo(baoZi);
        ch.start();
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/qq_44660367/article/details/109145319