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]