生产者/消费者模式
有两个厨师在做蛋糕(生产者)两个客人在吃蛋糕(消费者)
厨师做好了就放在桌子上,客人吃完了就从桌子上取,桌子实际上是共享队列
桌子最多能放3个蛋糕,放满了后厨师就等待 直到客人取走一个
整理后包括着几个类
MakerThread 厨师
EaterThread 客人
Table 共享队列
还有就是蛋糕了 我们用String来表示
package com.justel.fs.prod_cons;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 生产者
* @author 徐良永
* @date 2013-6-7下午1:17:06
*/
public class MakerThread extends Thread{
private static AtomicInteger i = new AtomicInteger(0);
private Table table;
private Random random = new Random();
public MakerThread(Table table, String name){
super(name);
this.table = table;
}
public void run(){
try {
while(true){
String cake = "蛋糕"+i.getAndIncrement();
table.put(cake);
System.out.println(Thread.currentThread().getName() + "正在做:" + cake);
Thread.sleep(random.nextInt(500));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.justel.fs.prod_cons;
import java.util.Random;
/**
* 消费者
* @author 徐良永
* @date 2013-6-7下午1:17:36
*/
public class EaterThread extends Thread{
private Table table;
Random random = new Random();
public EaterThread(Table table, String name){
super(name);
this.table = table;
}
public void run(){
try {
while (true) {
String cake = table.get();
System.out.println(Thread.currentThread().getName() + "正在吃:" + cake);
//random.nextInt(10000);1000
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 共享队列
* @author 徐良永
* @date 2013-6-7下午1:18:10
*/
public class Table {
private LinkedList<String> linkedList = new LinkedList<>();
public synchronized String get() throws InterruptedException{
while (linkedList.isEmpty()) {
wait(); //队列空了 所有消费线程转入等待状态
}
String s = linkedList.removeFirst();
notifyAll(); //通知所有生产线程---队列有空位了
return s;
}
public synchronized void put(String s)throws InterruptedException{
while(linkedList.size() >= 3){
System.out.println("-----------桌子已经放满了------------");
wait(); //队列满了 生产线程转为等待状态
}
linkedList.add(s);
notifyAll(); //通知所有消费线程---队列有数据了
}
}
public class Main {
public static void main(String[] args) {
Table table = new Table();
for (int i = 0; i < 3; i++) {
new EaterThread(table, "吃货" + i).start();
new MakerThread(table, "厨师" + i).start();
}
}
}
分享到:
相关推荐
在Linux操作系统下用C或C++实现经典同步问题:生产者-消费者问题。 含源代码和文档。 内容: 1.一个大小为10的缓冲区,初始状态为空。 2.2个生产者,随机等待一段时间,往缓冲区中添加数据,若缓冲区已满,等待消费...
producer-consumer-model 生产者-消费者模型
代码在这里
NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/1721530
本文为文章 https://blog.csdn.net/u012779110/article/details/104312468 的代码示例,供参考学习用。
Simple application demonstrate kafka java springboot
利用信号量实现的多线程之间的同步与互斥,详情看博客文章Linux多线程编程(二)---线程之间的同步与互斥进阶实验
single-producer, single-consumer lock-free queue
kettle kafka 消费者插件,在plugins 下新建steps文件夹,把zip文件解压放到里面。
* The classic producer-consumer example. * Illustrates mutexes and conditions. * by Zou jian guo <ah_zou@tom.com> * 2003-12-22 * *************************************************/ #include #...
9号实验室工作 任务 使用生产者-消费者模板实现搜寻器。 爬网程序是一种旨在对Internet页面进行... producer的数量由network_threads命令行network_threads parser_threads , consumer的数量parser_threads参数par
非常好的程序资源,一秒钟一千多条!
在讲完Looper,Handler和HandlerThread一章之后,一位学生问我们如何将流数据(从传感器)从后台线程传递回主线程(UI)? 对于此用例, HandlerThread原因如下: 我们的后台线程只是在做一件事:读取传感器数据。...
Kafka-Simple-Producer-Consumer:使用Java 8的kafka的生产者和消费者的简单变化
Algorithm for Producer-Consumer PoolsElad Gidron CS DepartmentTechnion, Haifa, Israel eladgi@cs.technion.ac.ilIdit Keidar EE DepartmentTechnion, Haifa, Israel idish@ee.technion.ac.ilDmitri Perelman ∗...
JAVA中生产者-消费者的线程JAVA中Threads+Producer-Consumer的使用演示要测试它,只需在 Java IDE 中打开它并运行 Main 类。
用PHP实现Queue-Producer-Consumer Web Crawler。通过amphp / parallel依赖项使用多个进程或本机线程来爬网域以获取响应者链接。 / _ \ \_\(_)/_/ _//o|\_ / | @作者:罗伯特·伯恩斯@电子邮件: 安装 使用'...
Spring-cloud-contract-producer-kafka-consumer Spring云合约生产商kafka消费者集成测试
生产者-消费者模式的 Java 8 变体
The use of producer-consumer questions helps to better understand threads.