The recent need to use multithreading to implement a feature in a project is similar to the producer consumer model, thus learning the multi-threaded implementation of the producer consumer model. In the producer consumer model, there are usually two types of threads,
That is, a number of 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 task submitted by the producer. The producer and consumer communicate through a shared memory buffer.
Here we choose Blockingqueue as the shared memory buffer.
First, we build the data pcdata that producers produce, and consumers need to deal with, that is, related task data.
public class PCData {
private final int intdata;
Public PCData (int d) {
Intdata = D;
}
Public PCData (String d) {
Intdata = integer.valueof (d);
}
public int GetData () {
return intdata;
}
@Override
Public String toString () {
Return "data:" +INTDATA;
}
}
Next, implement the producer thread, which builds the Pcdata object and puts it into the blockingqueue queue.
public class Producer implements Runnable {
Private volatile Boolean isrunning = true;
Private blockingqueue<pcdata> queue;
private static Atomicinteger count = new Atomicinteger (); Total, atomic operations
private static final int sleeptime = 1000;
Public Producer (blockingqueue<pcdata> queue) {
This.queue = queue;
}
@Override
public void Run () {
PCData data = null;
Random random = new random ();
System.out.println ("Start produce id =" + thread.currentthread (). GetId ());
while (isrunning) {
try {
Thread.Sleep (Random.nextint (sleeptime));
data = new PCData (Count.incrementandget ());
SYSTEM.OUT.PRINTLN (Data + "is put into queue");
if (!queue.offer (data, 2, timeunit.seconds)) {
System.err.println ("Failed to put data:" + data);
}
} catch (Interruptedexception e) {
E.printstacktrace ();
Thread.CurrentThread (). interrupt ();
}
}
}
public void Stop () {
IsRunning = false;
}
}
Next, implement the consumer, which removes the Pcdata object from the Blockingqueue queue for processing:
public class Comsumer implements Runnable {
Private blockingqueue<pcdata> queue;
private static final int sleeptime = 1000;
Public Comsumer (blockingqueue<pcdata> queue) {
This.queue = queue;
}
@Override
public void Run () {
System.out.println ("Start comsume id =" + thread.currentthread (). GetId ());
Random random = new random ();
try {
while (true) {
PCData data = Queue.take ();
if (null! = data) {
int re = Data.getdata () * Data.getdata ();
System.out.println (Messageformat.format ("{0}*{1}={2}", Data.getdata (),
Data.getdata (), re));
Thread.Sleep (Random.nextint (sleeptime));
}
}
}catch (Interruptedexception e) {
E.printstacktrace ();
Thread.CurrentThread (). interrupt ();
}
}
}
Finally, in the main function, we create three producers and three consumers, and let them work together.
public class Maintest {
public static void Main (string[] args) throws interruptedexception{
blockingqueue<pcdata> queue = new linkedblockingdeque<> (10);
Producer Pro1 = new Producer (queue);
Producer Pro2 = new Producer (queue);
Producer Pro3 = new Producer (queue);
Comsumer coms1 = new Comsumer (queue);
Comsumer coms2 = new Comsumer (queue);
Comsumer coms3 = new Comsumer (queue);
Executorservice service = Executors.newcachedthreadpool ();
Service.execute (Pro1);
Service.execute (PRO2);
Service.execute (PRO3);
Service.execute (COMS1);
Service.execute (COMS2);
Service.execute (COMS3);
Thread.Sleep (10*1000);
Pro1.stop ();
Pro2.stop ();
Pro3.stop ();
Thread.Sleep (3000);
Service.shutdown ();
}
}
Multi-thread producer consumer mode