The producer-consumer model is a classic multithreaded design pattern that provides a good solution for multithreaded collaboration. In producer-consumer mode, there are usually two types of threads, that is, several producer threads and several consumer threads. The producer thread is responsible for submitting the user request, and the consumer thread is responsible for processing the user request. Communication between producer and consumer through a shared memory buffer.
The main function of the memory buffer in producer-consumer mode is the sharing of data between threads. In addition, this buffer can mitigate the poor performance between producers and consumers.
In Java multithreaded programming, common multithreaded design patterns include: Future mode, Master-worker mode, guarded suspeionsion mode, invariant mode and producer-consumer model. This article mainly describes the invariant mode, the address of other multithreaded design patterns is as follows:
A detailed explanation of the future mode: A detailed explanation of future mode in Java multithreaded programming
A detailed explanation of the Master-worker mode: A detailed explanation of Master-worker mode in Java multithreaded programming
A detailed explanation of guarded suspeionsion mode: A detailed explanation of guarded suspeionsion mode in Java multithreaded programming
About the invariant mode of the detailed: Java multithreaded programming in the invariant mode of the detailed
Following the producer to produce steamed bread, consumers eat steamed bread as an example of producer-consumer model, and Java code to achieve.
1. Steamed bun
Here uses the object-oriented design idea, the steamed bun in the whole program naturally is a class, its Java code is as follows.
Class Mantou {
int id;
Mantou (int id) {
this.id = ID;
}
Public String toString () {return
"Mantou:" + ID;
}
}
2. Basket (steamed bun)
Baskets are of a capacity, so the basket class needs to have a variable to represent the capacity; the basket has at least the two functions of taking and loading of steamed buns, with Java code as follows:
Class Syncstack {//basket
int index = 0;
mantou[] arrmt = new Mantou[6]; Suppose this basket can only be packed with 6 buns public
synchronized void push (Mantou wt) {//steamed bun while
(index = arrmt.length) {//Basket
Full try { C6/>this.wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
This.notify (); If you take the steamed bread, then notify him to wake up
arrmt[index] = wt;
Index + +;
}
Public synchronized Mantou Pop () {//Take steamed bun while
(index = 0) {//Basket empty
try {
this.wait ();
} catch (Interr Uptedexception e) {
e.printstacktrace ();
}
}
This.notify (); If the steamed bun, then notify him to wake up
index--;
return arrmt[index];
}
3. Producers (production of steamed buns)
The producer needs to get the basket of this object, and the basket cannot be created by itself. Its Java code is as follows:
Class Producer implements Runnable {//producer
Syncstack SS = null;
Producer (Syncstack ss) {
THIS.SS = ss;
}
The public void Run () {for
(int i=0; i<20; i++) {//is to generate 20
mantou wt = new Mantou (i) altogether;
Ss.push (WT);
System.out.println ("produced:" + wt);
try {//Generate a 1-second sleep, easy to observe
Thread.Sleep ((Long) (Math.random () * 1000);
} catch (Interruptedexception e) {
E.printstacktrace ();}}}
4. Consumers (eat steamed bread)
Consumption needs to get the basket object, and this object must be the same object as the basket the producer obtains, so that information can be shared. Its Java code is as follows:
Class Consumer implements Runnable {
syncstack ss = null;
Consumer (Syncstack ss) {
THIS.SS = ss;
}
The public void Run () {for
(int i=0; i<20; i++) {//is to eat 20
mantou wt = SS.POP ();
System.out.println ("Consumption:" + wt);
}}
5. Test Run
The main function Java code is as follows:
public class Producerconsumer {public
static void Main (string[] args) {
Syncstack ss = new Syncstack ();//define Basket
producer p = new Producer (ss); Define producer
Consumer C = new Consumer (ss); Define consumer
new Thread (P). Start ();
New Thread (c). Start ();
}
}
Run the results above and print in the background as follows:
Complete the full text. Reprint please indicate the source.
Reference documents
1. Ge Yi, java Program Performance optimization. Tsinghua University Press.
2. Beijing still school Counseling organization Network video.