Usage example, based on a typical producer-consumer scenario. Note that a BlockingQueue can safely be used with multiple producers and multiple consumers.
class Producer implements Runnable { private final BlockingQueue queue; Producer(BlockingQueue q) { queue = q; } public void run() { try { while (true) { queue.put(produce()); } } catch (InterruptedException ex) { ... handle ...} } Object produce() { ... } } class Consumer implements Runnable { private final BlockingQueue queue; Consumer(BlockingQueue q) { queue = q; } public void run() { try { while (true) { consume(queue.take()); } } catch (InterruptedException ex) { ... handle ...} } void consume(Object x) { ... } } class Setup { void main() { BlockingQueue q = new ArrayBlockingQueue(); Producer p = new Producer(q); Consumer c1 = new Consumer(q); Consumer c2 = new Consumer(q); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); } }
Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a BlockingQueue
happen-before actions subsequent to the access or removal of that element from theBlockingQueue
in another thread.
BlockingQueue是Java Collection框架的一个接口,下面有很多实现类,包括:
ArrayBlockingQueue, (这个要自己会实现)
不用BlockingQueue实现的话,可以这么做:
public class Drop { // Message sent from producer // to consumer. private String message; // True if consumer should wait // for producer to send message, // false if producer should wait for // consumer to retrieve message. private boolean empty = true; public synchronized String take() { // Wait until message is // available. while (empty) { try { wait(); } catch (InterruptedException e) {} } // Toggle status. empty = true; // Notify producer that // status has changed. notifyAll(); return message; } public synchronized void put(String message) { // Wait until message has // been retrieved. while (!empty) { try { wait(); } catch (InterruptedException e) {} } // Toggle status. empty = false; // Store message. this.message = message; // Notify consumer that status // has changed. notifyAll(); } } public class Producer implements Runnable { private Drop drop; public Producer(Drop drop) { this.drop = drop; } public void run() { String importantInfo[] = { "Mares eat oats", "Does eat oats", "Little lambs eat ivy", "A kid will eat ivy too" }; Random random = new Random(); for (int i = 0; i < importantInfo.length; i++) { drop.put(importantInfo[i]); try { Thread.sleep(random.nextInt(5000)); } catch (InterruptedException e) {} } drop.put("DONE"); } } public class Consumer implements Runnable { private Drop drop; public Consumer(Drop drop) { this.drop = drop; } public void run() { Random random = new Random(); for (String message = drop.take(); ! message.equals("DONE"); message = drop.take()) { System.out.format("MESSAGE RECEIVED: %s%n", message); try { Thread.sleep(random.nextInt(5000)); } catch (InterruptedException e) {} } } } public class ProducerConsumerExample { public static void main(String[] args) { Drop drop = new Drop(); (new Thread(new Producer(drop))).start(); (new Thread(new Consumer(drop))).start(); } }
以上代码来自:
相关推荐
ProducerConsumer(生产者消费者问题的单线程模拟) vs2008
ProducerConsumer-CSharp
57119101_王晨阳_ProducerConsumer1
非常好的程序资源,一秒钟一千多条!
The use of producer-consumer questions helps to better understand threads.
1、n个缓冲区的缓冲池作为一个临界资源:当生产者任务从数据源—文件中读取数据后将会申请一个缓冲区,并将此数据放缓冲区中。消费者任务从一个缓冲区中取走数据,并将其中的内容打印输出。当一个生产者任务正在访问...
生产者消费者代码
Flex基于Producer和Consumer方式的简易消息推送机制
多线程练习 Java MultiThread练习代码,包括ReaderWriter,BlockingQueue,ProducerConsumer等的实现。
别人的太贵了,自己写一份分享!c语言实现生产者消费者模型,支持设置生产者消费者线程数量,支持设置消息数量。消息的组织形式为链表。生产者生产消息和消费者处理消息分别需要对应的条件变量。...
在Linux操作系统下用C或C++实现经典同步问题:生产者-消费者问题。 含源代码和文档。 内容: 1.一个大小为10的缓冲区,初始状态为空。 2.2个生产者,随机等待一段时间,往缓冲区中添加数据,若缓冲区已满,等待消费...
NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/1721530
主要介绍了Springboot集成Kafka实现producer和consumer的示例代码,详细的介绍了什么是Kafka和安装Kafka以及在springboot项目中集成kafka收发message,感兴趣的小伙伴们可以参考一下
kafka集群搭建,开启sasl认证,并通过Python调用Producer和Consumer
代码在这里
c语言。生产者消费者问题的信号量解决代码。简洁明了
在这个系统中,Producer/Generator 将发送一系列随机算术表达式(为了好玩而采用反向波兰表示法)。 Consumer/Evaluator 将接受这些表达式,计算结果,然后将解决方案报告给 Generator 或任何正在倾听的人。 ...
生产者/消费者问题使用(POSIX 信号量和线程) 获取文件: 在实验室计算机上,使用以下命令: ...课程: 创建和管理 POSIX 线程和信号量。 从多个 POSIX 线程访问共享(进程内)内存。 美味的早餐麦片... ...
rabbit_mq::rabbit:建立一致且均衡的ProducerConsumer管道
producer_consumer_os 操作系统中的生产者消费者问题