Use Linkedblockingqueue in Java to implement producer, consumer mode
Linkedblockingqueue implementation is thread-safe, which realizes FIFO (first-in first out) and other characteristics. is the first choice as producer consumers, Linkedblockingqueue can specify capacity, or can not be specified, not specified, the default is the largest integer.max_value, which mainly used put and take method, The put method blocks until a queue member is consumed when the queue is full, and the take method blocks when the queue is empty until a queue member is brought in.
Do not repeat the words in the book, or look at the example code.
Factory production and production of large sprinkle, there are beautiful women.
The consumer has the X second generation, also has the director.
Let consumers rob Resources.
Producer Realization
Package Cn.hpc.producerConsumer;
Import Java.util.UUID;
Import Java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
private blockingqueue<string> queue;
private String produce;
Public Producer (blockingqueue<string> queue, String produce) {
this.queue = queue;
if (null!= produce)
this.produce = produce;
else This.produce = "null";
}
@Override public
Void Run () {
String uuid = Uuid.randomuuid (). toString ();
try {
thread.sleep (200);//production takes time
queue.put (produce + ":" + uuid);
SYSTEM.OUT.PRINTLN ("Produce \" "+ Produce +" \ ":" + uuid + "" + Thread.CurrentThread ());
} catch (Interruptedexception e) {
System.out.println (e.getmessage ());
}
}
}
Consumers
Package Cn.hpc.producerConsumer;
Import Java.util.concurrent.BlockingQueue;
public class Consumer implements Runnable {
private blockingqueue<string> queue;
private String consumer;
Public Consumer (blockingqueue<string> queue, String Consumer) {
this.queue = queue;
if (null!= consumer)
This.consumer = consumer;
else
This.consumer = "null";
}
@Override public
Void Run () {
try {
String uuid = Queue.take ();
SYSTEM.OUT.PRINTLN (consumer + "decayed" + uuid
+ "" + Thread.CurrentThread ());
} catch (Interruptedexception e) {
System.out.println (e.getmessage ());
}
}
}
Call: New Tester ();
Do
Package Cn.hpc.producerConsumer;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.LinkedBlockingQueue;
public class Tester {public
Tester () {
//queues
linkedblockingqueue<string> queue = new Linkedblockingqueue<string> (ten);
Executorservice service = Executors.newcachedthreadpool ();
for (int i = 0; i < 6; i++) {
service.submit (new Consumer (Queue, "x second-generation" + i));
Service.submit (new Consumer (queue, "director" + i));
}
for (int i = 0; i < 6; i++) {
service.submit (new Producer (Queue, "golden wine," + i));
Service.submit (new Producer (queue, "Belle actress" + i));
}
Service.shutdown ();
}
Look at the output log
12-26 12:13:07.689:i/system.out (19372): Produce "Golden Wine 0": 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[ Pool-1-thread-13,5,main] 12-26 12:13:07.699:i/system.out (19372): Director 4 decayed actress 4: 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-10,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "Golden Wine 1": 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-15,5,main] 12-26 12:13:07.699:i/ System.out (19372): X second-generation 5 decayed Gold Wine 5:66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-11,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "Golden Wine 5": 66d5449b-ad38-41fe-8012-224b0f996697 thread[pool-1-thread-23,5, Main] 12-26 12:13:07.699:i/system.out (19372): Director 5 decayed Belle actress 5:d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[ Pool-1-thread-12,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "Belle actress 1": 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-16,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "beautiful actress 3": 72fcbcec-c903-4310-886a-40e31c693248 ThreaD[pool-1-thread-20,5,main] 12-26 12:13:07.699:i/system.out (19372): X second-generation 2 decayed Gold Wine 2: 75F1952C-975D-41B1-BA69-9E24006031CD Thread[pool-1-thread-5,5,main] 12-26 12:13:07.699:i/system.out (19372): Director 2 Decayed Belle actress 2:adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-6,5,main] 12-26 12:13:07.699:i/system.out ( 19372): Director 3 decayed Belle actress 3:72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-8,5,main] 12-26 12:13:07.699:i/ System.out (19372): Produce "Golden Wine 2": 75F1952C-975D-41B1-BA69-9E24006031CD Thread[pool-1-thread-17,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "beautiful actress 0": aad2007e-6322-43bc-a1f3-ade9af671e7b thread[pool-1-thread-14,5, Main] 12-26 12:13:07.699:i/system.out (19372): X second-generation 0 decayed gold wine 0:67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[ Pool-1-thread-1,5,main] 12-26 12:13:07.699:i/system.out (19372): Director 0 decayed actress 0: aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-2,5,main] 12-26 12:13:07.699:i/system.out (19372): X second-generation 1 Decayed Golden Wine 1:21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-3,5,main] 12-26 12:13:07.699:i/system.out (19372): Director 1 decayed Belle actress 1: 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-4,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "Golden Wine 3": C0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-19,5,main] 12-26 12:13:07.699:i/ System.out (19372): Produce "beautiful actress 5": d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-24,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "Golden Wine 4": 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 thread[pool-1-thread-21,5, Main] 12-26 12:13:07.699:i/system.out (19372): Produce "beautiful actress 2": Adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[ Pool-1-thread-18,5,main] 12-26 12:13:07.699:i/system.out (19372): Produce "Belle actress 4": 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-22,5,main] 12-26 12:13:07.699:i/system.out (19372): X second-generation 3 Decayed Golden Wine 3:c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-7,5,main] 12-26 12:13:07.699:i/system.out ( 19372): X second-generation 4 decayed Golden Wine 4:272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-9,5,main]