博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ReentrantLock Condition 实现消费者生产者问题
阅读量:4577 次
发布时间:2019-06-08

本文共 3339 字,大约阅读时间需要 11 分钟。

import java.util.LinkedList;import java.util.Queue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;//产品class Product{	 String name=null;	public Product(String name)	{		this.name=name;	}	}class  Buffer{  private Queue
queue=new LinkedList
();//一个普通队列 private final int size=5; //最大长度为,可以自己调整 public void add(Product p)// { synchronized (queue) { while(queue.size()==size) { System.out.println(Thread.currentThread().getName()+"队列已经满了,生产者释放锁"); try { queue.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } queue.offer(p); System.out.println(Thread.currentThread().getName()+"入队 "+queue.size()); queue.notify(); } } public void remove() { synchronized (queue) { while(queue.size()<=0) { System.out.println(Thread.currentThread().getName()+"队列为空,释放锁"); try { queue.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } queue.poll(); System.out.println(Thread.currentThread().getName()+"出队,剩下"+queue.size()); queue.notify(); } } }class Buffer2{ private Queue
queque=new LinkedList
();private int size=5;private ReentrantLock lock=new ReentrantLock(true);private Condition notFull=lock.newCondition();private Condition notEmpty=lock.newCondition();public void add(Product p){ lock.lock(); try { while(queque.size()==size) { notFull.await(); } queque.add(p); notEmpty.signal(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { lock.unlock(); } }public void remove() { try { lock.lockInterruptibly(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while(queque.size()==0) notEmpty.await(); queque.poll(); notFull.signal(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ lock.unlock(); } } }class Producer implements Runnable{ private Buffer2 buf; public Producer(Buffer2 buf) { this.buf=buf; } @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++) { try { Thread.sleep(3000); //控制生产速度 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } buf.add(new Product("zhang "+i)); } } }class Customer implements Runnable{ private Buffer2 buf=null; public Customer(Buffer2 buf) { this.buf=buf; } @Override public void run() { for(int i=0;i<10;i++) { try { Thread.sleep(1);//控制生产速度,, } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }// buf.remove(); } } }public class 生产消费者 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //学学使用线程池 Buffer2 buf=new Buffer2(); ExecutorService exe=Executors.newCachedThreadPool(); int i=0; while(i++<2) { exe.submit(new Producer(buf)); } i=0; while(i++<2) { exe.submit(new Customer(buf)); } exe.shutdown(); }}

  

转载于:https://www.cnblogs.com/hansongjiang/p/3870332.html

你可能感兴趣的文章
我选择的……
查看>>
akka actor初探
查看>>
linux清理Java环境
查看>>
如何更改webstrom的默认端口63342
查看>>
最短路计数
查看>>
SharedPreferences
查看>>
Android性能优化方法(六)
查看>>
yii2.0 报错Cookievalidationkey Must Be Configured With A Secret Key
查看>>
JQ在线引用地址
查看>>
TCP协议
查看>>
高级IO-锁与进程和文件
查看>>
对象在内存中的布局-对象的创建
查看>>
FZU 1077 铁皮容器 【枚举/二分】
查看>>
uva 11795 Mega Man's Mission(动态规划-状态压缩DP)
查看>>
MATLAB实现曲线拟合
查看>>
html总结
查看>>
WPF RichTextBox,关键字搜索,样式改变,超链接替换,图文混排
查看>>
gc日志分析
查看>>
数据结构--栈的思想与数组实现
查看>>
javascript的构造函数和原型
查看>>