多线程 生产者消费者模式 线程之间的通讯问题
2017-04-15 15:11
369 查看
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 SomeQueueImplementation();
* 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();
* }
* }
* 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 SomeQueueImplementation();
* 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();
* }
* }
相关文章推荐
- [多线程] ManualResetEvent(线程之间通讯 通知或者查看是否完成)
- Android面试篇之多线程[线程之间的通讯]
- java 中多线程之间的通讯之生产者和消费者 (多个线程之间的通讯)
- 线程开发之多线程之间的通讯实现
- 线程间的通讯和生产者和消费者之间的问题
- 线程开发之多线程之间的通讯实现
- Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型
- Java/Android多线程并发、同步,线程之间通信,主、子线程的一些问题(CountDownLatch、CyclicBarrier和Semaphore)
- C#多线程-不同线程之间通过事件委托封送调用方法
- 多线程之间的调剂问题
- java高级多线程编程--关于线程的停止问题
- 多线程编程之三——线程间通讯
- 服务器通讯之间的问题
- 线程之间的通讯---SynchronizationContext (转)
- 多线程编程之三——线程间通讯
- DLL与EXE之间的通讯调用 以及 回调函数的线程执行空间
- C#多线程-不同线程之间通过事件委托封送调用方法(转)
- 线程之间的通讯---SynchronizationContext
- 线程之间的通讯---SynchronizationContext
- java高级多线程编程--关于线程的停止问题