java multithreading implements producers and consumers

However, there are some mistakes in your understanding. Please also point out that:

Factory category:

public class MyFactory {

    private int foodNum = 0;

    public int getFoodNum() {
        return foodNum;
    }

    public void setFoodNum(int foodNum) {
        this.foodNum = foodNum;
    }

    public void consume(){
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.foodNum--;
    }

    public void produce(){
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.foodNum++;
    }
}
Consumer category:

public class ConsumerThread implements Runnable {
    private MyFactory myFactory =null;

    public ConsumerThread(MyFactory factory){
        this.myFactory = factory;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {

            synchronized (this.myFactory){
                try{
                    Thread.sleep(10);
                    while(this.myFactory.getFoodNum() <= 0){
                        System.out.println("There is no stock in the warehouse. Please inform the producer to produce it.");
                        this.myFactory.wait();          //Abandoning the right to use the factory:Lock
                    }
                    myFactory.consume();
                    myFactory.notifyAll();             //Notify those blocked producer threads that they are ready to produce after consuming
                    System.out.println("After the end of the consumption, there will be more"+myFactory.getFoodNum()+"Parts");
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
Producers:

public class ProducerThread implements Runnable {
    private MyFactory myFactory = null;

    public ProducerThread (MyFactory factory){
        this.myFactory = factory;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            synchronized (this.myFactory){
                try{
                    Thread.sleep(100);
                    while (this.myFactory.getFoodNum() >= 10){//The stock is full.
                        System.out.println("Warehouse stock is 10 (full), enter the waiting state,Waiting for Consumer Cancellation Fee");
                        this.myFactory.wait();
                    }
                    myFactory.produce();
                    System.out.println("After production, there are"+myFactory.getFoodNum()+"Parts");
                    myFactory.notifyAll();//After production, the blocked consumer threads are notified that they can be consumed.
                }catch (InterruptedException e){
                    e.printStackTrace();
                }

            }
        }
    }
}
Test class:

/**
 * Testing uses wait and notify to simulate producers and consumers
 * Many sleep s are used to simulate uncertainties.
 * Created by amanicspater on 2017/7/26.
 */
public class Main {
    public static void main(String[] args) {
        MyFactory myFactory = new MyFactory();
        Thread producerthread = new Thread(new ProducerThread(myFactory));
        Thread consumerthread = new Thread(new ConsumerThread(myFactory));
        producerthread.start();
        consumerthread.start();
    }

}

Print results:
After production, there is a part.
There are two parts after production.
There are three parts after production.
Four parts after production
 There are five parts after production.
There are six parts after production.
There are seven parts after production.
There are eight parts after production.
There are nine parts after production.
There are 10 parts after production.
Warehouse inventory is 10 (full), enter the waiting state, wait for consumer cancellation fee
 There are nine spare parts after consumption.
There are eight spare parts left after consumption.
There are seven spare parts after consumption.
There are six spare parts left after consumption.
There are five spare parts left after consumption.
Four more parts after consumption
 There are three spare parts after consumption.
There are two spare parts after consumption.
There's still one spare part after consumption.
There are still 0 spare parts after consumption.
There is no stock in the warehouse. Please inform the producer to produce it.
After production, there is a part.
There are two parts after production.
There are three parts after production.
Four parts after production
 There are five parts after production.
There are six parts after production.
There are seven parts after production.
There are eight parts after production.
There are nine parts after production.
There are 10 parts after production.
There are nine spare parts after consumption.
There are eight spare parts left after consumption.
There are seven spare parts after consumption.
There are six spare parts left after consumption.
There are five spare parts left after consumption.
Four more parts after consumption
 There are three spare parts after consumption.
There are two spare parts after consumption.
There's still one spare part after consumption.
There are still 0 spare parts after consumption.

There was a random promised confusing print result before, and then it was impossible to type it out randomly.

Added by released on Mon, 10 Jun 2019 04:01:01 +0300