Java producer and consumer issues

It related to the principle of communication between threads:

wait():当前线程挂起并放弃CPU,同步资源,使别的线程可访问并修改共享资源,当前线程排队等候再次对资源访问。
notify():唤醒正在排队等待同步资源的线程中优先级最高者结束等待。
notifyAll():唤醒正在排队等待资源的所有线程结束等待。

Problem Scenario:

Producers (Producer) production of products, to the clerk (Clerk) where consumer (Customer) removed the product from the clerk there, the clerk can only hold a certain number of products (eg: 20), clerk if there has been 20 products, the clerk will notify producers pause (wait ()), the store put the goods free position, to inform producers to continue production, the clerk if there is no product, and will inform consumers wait, the store has products informing consumers continue to spend.

Example:

class Clerk { //店员
    int product;
    public synchronized void  AddProduct(){
        if(product<20){   //产品小于20时,继续生产产品
            product++;
            System.out.println(Thread.currentThread().getName()+"生产了第"+product+"个产品");
            notifyAll(); //产品为0时,消费者等待,产品product++,有产品了,通知消费者继续消费
        }else {  
            try {
                wait();    //产品大于等于20时,生产者等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void  ConsumeProduct(){
        if(product>0){  //产品大于0时,消费者取走产品
            System.out.println(Thread.currentThread().getName()+"消费了第"+product+"个产品");
            product--;
            notifyAll(); //产品为20时,生产者等待,产品product--,有产品的空位置时,通知生产者继续生产
        }else {
            try {
                wait();   //产品为0时,消费者等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Producer implements Runnable{//生产者
    Clerk clerk;
    public Producer(Clerk clerk){
        this.clerk=clerk;
    }
    @Override
    public void run() {
        while (true){
            clerk.AddProduct();
        }
    }
}

class Consumer implements Runnable{ //消费者
    Clerk clerk;
    public Consumer(Clerk clerk){
        this.clerk=clerk;
    }
    @Override
    public void run() {
        while (true) {
            clerk.ConsumeProduct();
        }
    }
}

Test categories:

public class Test {
    public static void main(String[] args) {
        Clerk clerk=new Clerk();
        Producer p1=new Producer(clerk);
        Consumer c1=new Consumer(clerk);

        Thread th1=new Thread(p1);
        Thread th2=new Thread(c1);
        th1.setName("生产者:");
        th2.setName("消费者:");

        th1.start();
        th2.start();
    }
}

Screenshot run some results:
Here Insert Picture Description

Released six original articles · won praise 3 · Views 107

Guess you like

Origin blog.csdn.net/qq_43788878/article/details/104061059