ReentrantLock Condition 实现消费者生产者问题
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<Product> queue=new LinkedList<Product>();//一个普通队列
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<Product> queque=new LinkedList<Product>();
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(); } }
ReentrantLock Condition 实现消费者生产者问题的更多相关文章
- Condition实现一个生产者一个消费者
Condition实现一个生产者一个消费者,实现一对一交替打印: import java.util.concurrent.locks.Condition; import java.util.concu ...
- 【爬虫】Condition版的生产者和消费者模式
Condition版的生产者和消费者模式 threading.Condition 在没有数据的时候处于阻塞状态,有数据可以使用notify的函数通知等等待状态的线程运作 threading.Condi ...
- java并发编程——通过ReentrantLock,Condition实现银行存取款
java.util.concurrent.locks包为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器.该框架允许更灵活地使用锁和条件,但以更难用的语法为代价. Lock 接口 ...
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
本篇我们将讨论以下知识点: 1.线程同步问题的产生 什么是线程同步问题,我们先来看一段卖票系统的代码,然后再分析这个问题: package com.zejian.test; /** * @author ...
- java 多线程 Thread 锁ReentrantLock;Condition等待与通知;公平锁
1,介绍: import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; 在JA ...
- Lock锁与Condition监视器(生产者与消费者)。
/*生产者与消费者第二次敲,本人表示很郁闷,以后要经常读这个 * Condition 将Object类中的监视器(wait notify notifyAll)分解成不同的对象.例如condition_ ...
- java 用condition&reentrylock实现生产者消费者
package com.lb; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks ...
- Java并发控制:ReentrantLock Condition使用详解
生产者-消费者(producer-consumer)问题,也称作有界缓冲区(bounded-buffer)问题,两个进程共享一个公共的固定大小的缓冲区.其中一个是生产者,用于将消息放入缓冲区:另外一个 ...
- java多线程之消费者生产者模式 (转)
/*@author shijin * 生产者与消费者模型中,要保证以下几点: * 1 同一时间内只能有一个生产者生产 生产方法加锁sychronized * 2 同一时间内只能有一个消费者消费 消费方 ...
随机推荐
- JavaScript之Chart.js图例(legend)
#html <div id="chart_line_legend" class="chart-legend"></div> <ca ...
- (转)myrepo
源作者主页:https://copr.fedoraproject.org/coprs/mosquito/myrepo/ 源作者github: https://github.com/1dot75cm/m ...
- jquery 多级无限分类
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- CSS3鼠标移入移出图片生成随机动画
今天分享使用html+css3+少量jquery实现鼠标移入移出图片生成随机动画,我们先看最终效果图(截图为静态效果,做出来可是动态的哟) 左右旋转 上下移动 缩放 由于时间关系我就不一步步解析各段代 ...
- 上传文件到服务器端后进一步推送到sftp服务器
扩展安装 要想sftp服务端发送文件,就需要php脚本具有作为ssh客户端的能力,所以需先为php安装如下扩展 openssl openssl-dev libssh php ssh 扩展 按照下面的命 ...
- Sublime Text 3 插件、主题、配置
换电脑,Sublime Text 3 重新配置一遍,做个记录 1. 下载:http://www.sublimetext.com/3 2. 插件管理器 Package Control (Ctrl + ` ...
- linux重新增加硬盘容量
1.先用df -h查看硬盘使用情况 2.fdisk -l查看分区情况 表示还没有挂载 3.fdisk /dev/vdb进行分区 4.mkfs.ext3 /dev/vdb进行格式化 5.mount /d ...
- 未能解析此远程名称:'nuget.org'
今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...
- 坑爹的NSIS转义符:$ (在NSIS的MessageBox中写换行文字)
最近的项目中,发现了NSIS一个比较坑的地方:$ 不但是变量常量的开头,还是一个转义字符. 大家有没有发现,NSIS写的脚本中,如果要让弹出消息框中的文字带换行功能,“\r\n” 是不是很不管用呢? ...
- iOS - Responder Chain
在iOS中,当发生事件响应时,必须知道由谁来响应事件.这就是由响应者链来对事件进行响应,所有事件响应的类都是UIResponder的子类,响应者链是一个由不同对象组成的层次结构,其中的每个对象将依次 ...