Production and consumption model
preface
When learning threads, I have learned the six states of threads:
- New: new state – the state of a thread when new is used to create a new thread or its subclass.
- Runnable: running state – the state of the thread that preempts the cpu resources when multiple threads preempt the resources.
- Blocked: blocked state – the state of threads without resources when multiple threads preempt cpu resources. When the cpu is idle, it will occupy the cpu and change to Runnable state.
- Terminated: dead state – the state of the thread where the execution of the run method ends or the stop method is called.
- Timed_Waiting: sleep state - the state of the thread that calls the wait or sleep method with parameters. After the time is over, if the cpu is idle, it will become runnable, and if the cpu is not idle, it will become blocked. You can be awakened yourself.
- Waiting: infinite wait state – the state of the thread that calls the parameterless wait method and will wait to be awakened all the time. Wake up through the notify method. Don't wake yourself up.
Switching between the waiting state and the awakened state, there is a communication model between threads called the production consumer model. This article code implements a production consumer model.
1, Production consumer model
Suppose there are three parts: producer, warehouse and consumer. If there is no product in the warehouse, the consumer wakes up the producer, tells the producer that it needs to produce products, wakes up the producer model, and then gives up the cpu execution right to enter the wait state to wait to be awakened. After receiving the production notice, the producer is awakened to produce products, and then awaken consumers to use products. If the number of products reaches the upper limit of warehouse capacity, the producer will enter the wait state and wait to be awakened.
Note that the wait and notify methods should be called by the same object. Here, select the warehouse object to call.
2, Code implementation
Producer model:
public class Producer implements Runnable { private final ArrayList<Integer> warehouse; private final int maxSize; public Producer(ArrayList<Integer> warehouse, int maxSize) { this.warehouse = warehouse; this.maxSize = maxSize; } @Override public void run() { while (true) { synchronized (warehouse) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (warehouse.size() > maxSize-1) { try { System.out.println("There are in the warehouse"+warehouse.size()+"Product, producer model entry wait"); warehouse.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Start production"); }else{ warehouse.add(warehouse.size()+1); System.out.println("Produced the second"+warehouse.size()+"Products"); warehouse.notify(); // System.out.println("wake up consumers and start consuming products"); } } } } }
Consumer model:
public class Customer implements Runnable{ private final ArrayList<Integer> warehouse; public Customer(ArrayList<Integer> warehouse) { this.warehouse = warehouse; } @Override public void run() { while (true) { synchronized (warehouse) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (warehouse.size() <=0) { warehouse.notify(); System.out.println("Tell the producer to start production"); try { System.out.println(); System.out.println("Consumer entry wait "); warehouse.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }else{ System.out.println(warehouse.size()); int re=warehouse.remove(warehouse.size()-1); System.out.println("The consumer has consumed the second"+re+"Piece product"); } } } } }
Main program:
public class Manage { //Create warehouse object public static ArrayList<Integer> warehouse=new ArrayList<>(); public static int maxSize=20; public static void main(String[] args) { Producer producer=new Producer(warehouse, maxSize); Customer customer=new Customer(warehouse); Thread producerThread=new Thread(producer); producerThread.start(); Thread customerThread=new Thread(customer); customerThread.start(); } }
After testing, the above code can successfully realize the production and consumption model.