基于BlockingQueue的生产者-消费者模型
不管是在学习操作系统知识还是Java多线程知识的时候,都会遇到生产者-消费者模型。我们必须熟练地写出一个简单的模型。之前的使用的大多数都是synchronized锁同步代码块或修饰方法或是使用ReetrantLock来完成,这里介绍使用java.util.concurrent包提供的BlockingQueue来实现。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
class CakeShop{
//使用volatile修饰,保证多个线程之间的可见性
private volatile boolean FLAG = true;
private AtomicInteger cake = new AtomicInteger(0);
private BlockingQueue<String> blockingQueue;
//传参传接口,方便多种不同的需求
CakeShop(BlockingQueue<String> blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void production(){
String data;
boolean retValue = false;
//多线程情况下,使用while进行条件判断
while (FLAG) {
data = cake.incrementAndGet() + "";
try {
retValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (retValue) {
System.out.println(Thread.currentThread().getName() + " 生产队列,生产蛋糕" + data + ",成功");
} else {
System.out.println(Thread.currentThread().getName() + " 生产队列,生产蛋糕" + data + ",失败");
}
//模拟生产过程
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println((Thread.currentThread().getName() + " flag=false,停止生产!"));
}
public void consumption(){
String res = "";
while (FLAG) {
try {
res = blockingQueue.poll(2L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (res == null || res.equalsIgnoreCase("")) {
FLAG = false;
System.out.println(Thread.currentThread().getName() + " 超过两秒钟未取到蛋糕,停止消费");
System.out.println();
System.out.println();
return;
}
System.out.println(Thread.currentThread().getName() + " 消费队列,消费蛋糕" + res + ",成功!");
}
}
public void stop(){
this.FLAG = false;
}
}
public class ProdConsumerWithBlockingQueue {
public static void main(String[] args) {
CakeShop cakeShop = new CakeShop(new ArrayBlockingQueue<>(10));
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + " 启动");
cakeShop.production();
}, "生产者线程").start();
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + " 启动");
try {
cakeShop.consumption();
System.out.println();
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}, "消费者线程").start();
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("============================");
System.out.println("五秒钟时间到,停止活动!");
cakeShop.stop();
}
}
上一篇:
IDEA上Java项目控制台中文乱码
