The problem of consumers and producers is a common problem in computers, and it is a classic problem of multithreading in java
1.java multithreading implementation
Method of 「 wait() / notify() 」
The wait() / nofit() method is two methods of the base class Object:
- wait() method: when the buffer is full / empty, the producer / consumer thread stops its execution, abandons the lock, keeps itself in a state of wait, and lets other threads execute.
- notify() method: when the producer / consumer puts / takes a product into / out of the buffer, it will send an executable notification to other waiting threads, and at the same time, it will give up the lock to make itself in the waiting state.
import java.util.LinkedList; public class produceAndCustomer { //Maximum number of products private final int MAX_LIST_SIZE = 100; //Product collection store, which needs frequent insertion and deletion, uses LinkedList. private LinkedList<Object> itemList = new LinkedList<>(); //Producers produce goods //Producers are not allowed to produce when the commodity collection is full public void produce(String produceName) { synchronized (itemList) { while (itemList.size() == MAX_LIST_SIZE) { System.out.println("The factory is full: unable to perform the production task temporarily!"); try { //When the length of commodity set is equal to the maximum length, the producer stops production itemList.wait(); } catch (InterruptedException e) { System.out.println(); e.printStackTrace(); } } //Production does not meet the total capacity of the plant and warehouse //Production commodity itemList.add(new Object()); System.out.println("The producer produces a commodity, and the current capacity of the factory is:"+itemList.size()); //Produce goods and arouse consumers' consumption itemList.notifyAll(); } } //Consumer goods //Consumers are not allowed to consume when the commodity set is empty public void customer(String customerName) { synchronized (itemList) { while (itemList.size()==0){ System.out.println("Factory library is empty: cannot perform consumption task temporarily!"); try { //When the factory library is empty, the consumer cannot consume, stop the thread itemList.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //The factory and warehouse are not empty and can be consumed itemList.removeFirst(); System.out.println("Consumers consume a commodity, and the current capacity of the factory is:"+itemList.size()); //Consumer goods, awaken producer production itemList.notifyAll(); } } //get and set operations of properties public LinkedList<Object> getItemList() { return itemList; } public void setItemList(LinkedList<Object> itemList) { this.itemList = itemList; } public int getMAX_LIST_SIZE() { return MAX_LIST_SIZE; } }
Test method:
public class Test { //Consumer and producer test methods public static void main(String[] args) { produceAndCustomer produceAndCustomer = new produceAndCustomer(); //Define 8 producers and execute 8 times of production for (int i = 0; i < 8; i++) { int num = i; //Using anonymous inner class to implement new Thread(new Runnable() { public void run() { produceAndCustomer.produce(String.format("Producer%d", num)); } }).start(); } //Define 4 consumers and execute 4 consumption for(int i=1;i<4;i++) { int num = i; //Using anonymous inner class to implement new Thread(new Runnable() { public void run() { produceAndCustomer.customer(String.format("Producer%d", num)); } }).start(); } } }
There's another way to lock
- await() / signal() method
The code is similar to the above, a variation of the wait() and notify() methods. Guys can try it on their own