生产者-使用者例子

本文通过三个示例展示了如何使用Java实现生产者-消费者模式,包括单生产者单消费者、单生产者多消费者及多生产者多消费者的场景,并介绍了如何利用BlockingQueue来简化线程间的同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

好久不写东西了,写点小东西吧,呵呵

这是一个简单的生产者-消费者例子(取自java 6.0 api介绍java.util.concurrent 接口 BlockingQueue时举的例子)

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(); 
   } 
} 

 


这个例子非常易于理解,特征明显,属于典型案例,但我们平时的应用一般不会无限循环下去,我们需要消费者在生产者结束后把queue中的数据获取出来、做完相应的操作以后结束生产消费过程。 针对该需求可以修改一下程序,做一个单生产者的例子,如下:

class Producer implements Runnable { 
   private final BlockingQueue queue; 
   Producer(BlockingQueue q) { queue = q; } 
   public void run() { 
     try { 
       while(生产条件) { queue.put(produce()); } 
     } catch (InterruptedException ex) { ... handle ...} 
   } 
   Object produce() { ... } 
} 

class Consumer implements Runnable { 
   private final BlockingQueue queue; 
   private Thread p; 
   Consumer(BlockingQueue q,Thread producer) { queue = q; p = producer;} 
   public void run() { 
     try { 
       while(!p.isAlive()||!queue.isEmpty()) {//循环直到生产者线程结束并且把queue中的数据全部获取完 
   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(); 
   } 
} 

 

有时候会有多生产者,再写一个多生产者的例子,如下:

public class Test { 

private ExecutorService producerPool = Executors.newFixedThreadPool(5); 

private ExecutorService consumerPool = Executors.newFixedThreadPool(5); 

private BlockingQueue queue = new ArrayBlockingQueue(100); 

public void t() { 

for (int i = 0; i  0) { 
String s = null; 
try { 
s = queue.take(); 
System.out.println("get a product:"+s); 
} catch (InterruptedException e) { 
e.printStackTrace(); 
} 
} 

System.out.println("consumer-" + Thread.currentThread().getName() + " is done!"); 
} 
}; 
consumerPool.submit(consumer); 
} 
consumerPool.shutdown(); 
} 

public static void main(String[] args){ 

new Test().t(); 

} 
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值