LinkedBlockingQueue實現是線程安全的,實現了FIFO(先進先出)等特性. 是作為生產者消費者的首選,LinkedBlockingQueue 可以指定容量,也可以不指定,不指定的話,默認最大是Integer.MAX_VALUE,其中主要用到put和take方法,put方法在隊列滿的時候會阻塞直到有隊列成員被消費,take方法在隊列空的時候會阻塞,直到有隊列成員被放進來。
書本上的話不再重復, 還是看看實例代碼.
工廠生產制造 生產高大上灑, 還有美女.
消費者有X二代,也有導演.
讓消費者搶資源吧.
生產者實現
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);//生產需要時間
queue.put(produce + " : " + uuid);
System.out.println("Produce \"" + produce + "\" : " + uuid + " " + Thread.currentThread());
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
消費者
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());
}
}
}
調用 : new Tester();
做
package cn.hpc.producerConsumer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class Tester {
public Tester(){
// 隊列
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(10);
ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < 6; i++) {
service.submit(new Consumer(queue, "X二代" + i));
service.submit(new Consumer(queue, "導演" + i));
}
for (int i = 0; i < 6; i++) {
service.submit(new Producer(queue, "黃金酒," + i));
service.submit(new Producer(queue, "美女演員" + i));
}
service.shutdown();
}
}
看看輸出日志
12-26 12:13:07.689: I/System.out(19372): Produce "黃金酒0" : 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[pool-1-thread-13,5,main]
12-26 12:13:07.699: I/System.out(19372): 導演4 decayed 美女演員4 : 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-10,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "黃金酒1" : 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-15,5,main]
12-26 12:13:07.699: I/System.out(19372): X二代5 decayed 黃金酒5 : 66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-11,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "黃金酒5" : 66d5449b-ad38-41fe-8012-224b0f996697 Thread[pool-1-thread-23,5,main]
12-26 12:13:07.699: I/System.out(19372): 導演5 decayed 美女演員5 : d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-12,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "美女演員1" : 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-16,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "美女演員3" : 72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-20,5,main]
12-26 12:13:07.699: I/System.out(19372): X二代2 decayed 黃金酒2 : 75f1952c-975d-41b1-ba69-9e24006031cd Thread[pool-1-thread-5,5,main]
12-26 12:13:07.699: I/System.out(19372): 導演2 decayed 美女演員2 : adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-6,5,main]
12-26 12:13:07.699: I/System.out(19372): 導演3 decayed 美女演員3 : 72fcbcec-c903-4310-886a-40e31c693248 Thread[pool-1-thread-8,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "黃金酒2" : 75f1952c-975d-41b1-ba69-9e24006031cd Thread[pool-1-thread-17,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "美女演員0" : aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-14,5,main]
12-26 12:13:07.699: I/System.out(19372): X二代0 decayed 黃金酒0 : 67cbb3c8-b72b-4bd5-9edc-f1c3314e9f50 Thread[pool-1-thread-1,5,main]
12-26 12:13:07.699: I/System.out(19372): 導演0 decayed 美女演員0 : aad2007e-6322-43bc-a1f3-ade9af671e7b Thread[pool-1-thread-2,5,main]
12-26 12:13:07.699: I/System.out(19372): X二代1 decayed 黃金酒1 : 21f150e3-7909-47c3-a5b1-31b4f4242446 Thread[pool-1-thread-3,5,main]
12-26 12:13:07.699: I/System.out(19372): 導演1 decayed 美女演員1 : 9786647d-c499-40be-9905-da767ae2fe88 Thread[pool-1-thread-4,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "黃金酒3" : c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-19,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "美女演員5" : d6008dee-c42f-4c09-8856-ae38ac64e104 Thread[pool-1-thread-24,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "黃金酒4" : 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-21,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "美女演員2" : adb8b376-83c4-487b-9af1-11c16d060ee4 Thread[pool-1-thread-18,5,main]
12-26 12:13:07.699: I/System.out(19372): Produce "美女演員4" : 73e6939e-287e-4dda-88ff-2a59871f8a41 Thread[pool-1-thread-22,5,main]
12-26 12:13:07.699: I/System.out(19372): X二代3 decayed 黃金酒3 : c0ff371e-473d-4af0-90a7-620fa67c22d8 Thread[pool-1-thread-7,5,main]
12-26 12:13:07.699: I/System.out(19372): X二代4 decayed 黃金酒4 : 272cb2a6-b1dd-44d8-8254-df4f6be59bd2 Thread[pool-1-thread-9,5,main]