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 同一时间内只能有一个消费者消费 消费方 ...
随机推荐
- c#根据文件大小显示文件复制进度条实例
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 完整的 AJAX 写法(支持多浏览器)
代码如下: <script type="text/javascript"> var xmlhttp; function Submit() { //1.创建 XMLHtt ...
- Unix环境高级编程学习笔记——fcntl
写这篇文正主要是为了介绍下fcntl,并将我自己在学习过程中的一些理解写下来,不一定那么官方,也有错误,希望指正,共同进步- fcntl: 一个修改一打开文件的性质的函数.基本的格式是 int fcn ...
- libz.so库分析
from:http://blog.chinaunix.net/uid-12773189-id-84605.html 1.查看库文件是由哪个软件包提供的空闲时打开/usr/lib目录(因为我知道这个目录 ...
- jquery实现可展开收缩的首页大图广告展示方式 泰山压顶代码 V2.0
把代码做成js网站进行统一调用 if (typeof jQuery == 'undefined') { document.writeln('<script type="text/jav ...
- 在线小词典(mysql扩展库操作)
输入英文查询中文 1.建表 create table words( id int primary key auto_increment, enWords varchar(32) not null, c ...
- XMLHttpRequest 使用概括
***********************************************XMLHttpRequest对象初始化:********************************* ...
- MySQL中的max_connections和max_user_connections 及 MySQL服务器最大连接数的合理设置
max_connections 是指整个mysql服务器的最大连接数: max_user_connections 是指每个数据库用户的最大连接数,比如:虚拟主机可以用这个参数控制每个虚拟主机用户的数据 ...
- Firebird 同一字段的多行合并为一行
Firebird 同一字段的多行合并为一行用LIST函数类似于MYSQL的GROUP_CONCAT. 具体用法如下: SELECT LIST(a.GG_NAME||':'||a.GG_VALUE) ...
- 如何开启PDO,PDO_MYSQL扩展
开启这个功能的具体方法就是设置php.ini文件,步骤如下: 1.查看public_html目录下没有php.ini文件,如果有的, 打开文件查找 extension=php_pdo_mysql.dl ...