- 浏览: 519690 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (161)
- 多线程与并发编程 (20)
- 算法和数据结构 (8)
- 缓存 (0)
- HttpClient (2)
- 疑难杂症 (11)
- Java内存管理 (17)
- 分布式开发 (14)
- Linux常用命令 (10)
- OSGI (0)
- UML (2)
- 趣味面试题 (5)
- 设计模式 (8)
- Java类加载 (2)
- JSTL (1)
- Web 服务器 (4)
- IT人生 (3)
- Java基础 (11)
- Flash技术 (7)
- 新知识 (3)
- 常用速备速查 (4)
- 版本控制 (1)
- Java集合工具类 (6)
- web前端技术 (1)
- 趣味话题 (1)
- 安全 (0)
- 架构设计 (5)
- Spring (4)
- 负载均衡技术 (2)
- 持久层技术 (2)
- MySQL单机多实例方案 (1)
- 收藏备用 (0)
- 性能优化 (3)
最新评论
-
liuwuhen:
...
Pushlet的工作原理 -
fbwfbi:
fengchuizhuming 写道楼主的完全正确。鉴定完毕楼 ...
硬件同步原语(CAS)理论 -
passerby_whu:
uule 写道这个测试后结果为:“testPageConten ...
FutureTask的使用方法和使用实例 -
fengchuizhuming:
楼主的完全正确。鉴定完毕
硬件同步原语(CAS)理论 -
edwardjuice:
FutureTask的使用方法和使用实例
“生产者-消费者-仓储”模型,包含三种角色:
1.生产者
2.消费者
3.仓库
离开了仓储,生产者消费者模型就显得没有说服力了。
对于此模型,应该明确一下几点:
1、生产者仅仅在仓储未满时候生产,仓满则停止生产。
2、消费者仅仅在仓储有产品时候才能消费,仓空则等待。
3、当消费者发现仓储没产品可消费时候会通知生产者生产。
4、生产者在生产出可消费产品时候,应该通知等待的消费者去消费。
下面的三个类分别代表了生产者,消费者和仓库
package producerconsumer;
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version producerconsumer; Producer.java
* 2010-3-3
**/
public class Producer extends Thread{
Integer producedNumber;
Ware ware;
public Producer(Integer producedNumber, Ware ware){
this.producedNumber = producedNumber;
this.ware = ware;
}
public void run(){
try{
while(true){
ware.put(producedNumber);
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version producerconsumer; Producer.java
* 2010-3-3
**/
public class Producer extends Thread{
Integer producedNumber;
Ware ware;
public Producer(Integer producedNumber, Ware ware){
this.producedNumber = producedNumber;
this.ware = ware;
}
public void run(){
try{
while(true){
ware.put(producedNumber);
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
package producerconsumer;
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version producerconsumer; Consumer.java
* 2010-3-3
**/
public class Consumer extends Thread{
Integer consumedNumber;
Ware ware;
public Consumer(Integer consumedNumber, Ware ware){
this.consumedNumber = consumedNumber;
this.ware = ware;
}
public void run(){
try{
while(true){
ware.take(consumedNumber);
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version producerconsumer; Consumer.java
* 2010-3-3
**/
public class Consumer extends Thread{
Integer consumedNumber;
Ware ware;
public Consumer(Integer consumedNumber, Ware ware){
this.consumedNumber = consumedNumber;
this.ware = ware;
}
public void run(){
try{
while(true){
ware.take(consumedNumber);
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
package producerconsumer;
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version producerconsumer; Ware.java
* 2010-3-3
**/
public class Ware {
int capacity;
int size;
public Ware(int capacity){
this.capacity = capacity;
}
public void take(int number) throws InterruptedException{
synchronized (this) {
while(number>size){
System.out.println(Thread.currentThread().getName()+"========The consumer number "+number+" is greater than the size=="+size+".Wait to put.");
this.wait();
}
size = size -number;
System.out.println(Thread.currentThread().getName()+"========Consumer the product========"+number+";The ware has the product======"+size);
this.notifyAll();
}
}
public void put(int number) throws InterruptedException{
synchronized (this) {
while(size+number>capacity){
System.out.println(Thread.currentThread().getName()+"========The put number "+number+" plus the current ware size=="+size +" is greater than capacity=="+capacity+".Wait to consumer.");
this.wait();
}
size = size +number;
System.out.println(Thread.currentThread().getName()+"========Produced the product========"+number+";The ware has the product======"+size);
this.notifyAll();
}
}
public synchronized int size() {
return size;
}
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version producerconsumer; Ware.java
* 2010-3-3
**/
public class Ware {
int capacity;
int size;
public Ware(int capacity){
this.capacity = capacity;
}
public void take(int number) throws InterruptedException{
synchronized (this) {
while(number>size){
System.out.println(Thread.currentThread().getName()+"========The consumer number "+number+" is greater than the size=="+size+".Wait to put.");
this.wait();
}
size = size -number;
System.out.println(Thread.currentThread().getName()+"========Consumer the product========"+number+";The ware has the product======"+size);
this.notifyAll();
}
}
public void put(int number) throws InterruptedException{
synchronized (this) {
while(size+number>capacity){
System.out.println(Thread.currentThread().getName()+"========The put number "+number+" plus the current ware size=="+size +" is greater than capacity=="+capacity+".Wait to consumer.");
this.wait();
}
size = size +number;
System.out.println(Thread.currentThread().getName()+"========Produced the product========"+number+";The ware has the product======"+size);
this.notifyAll();
}
}
public synchronized int size() {
return size;
}
下面代码大家想想:
synchronized (this) {
while(size+number>capacity){
System.out.println(Thread.currentThread().getName()+"========The put number "+number+" plus the current ware size=="+size +" is greater than capacity=="+capacity+".Wait to consumer.");
this.wait();
}
while(size+number>capacity){
System.out.println(Thread.currentThread().getName()+"========The put number "+number+" plus the current ware size=="+size +" is greater than capacity=="+capacity+".Wait to consumer.");
this.wait();
}
为什么不能改成if (size+number>capacity)?
考虑下面的情况:
capacity:50
current size:40
生产线程Thread-0 put 20+ current size 40 = 60,线程阻塞,并释放锁
生产线程Thread-1 获取监控器的锁,并put10+currnet size 40=50,size=50,唤醒所有阻塞的线程
如果是if:Thread-0往下执行:size=50+20=70,显然超过了仓库的最大库存,在这种情况下就出现问题,如果改成while,Thread 0 仍然会阻塞,不会超过仓库的最大库存。
发表评论
-
死锁实例
2011-05-19 14:21 1908下面这道题,是考死锁的,比较简单,想两个问题: 1.什么时候 ... -
Java存储模型
2011-05-18 13:29 01.什么是存储模型 没有适当的同步,编译器生成指令的次序,可 ... -
设计模式-组合模式
2011-05-16 15:48 1077组合模式的定义: 将对象组合成树的形式来表示整体和局部之 ... -
CompleteService介绍和使用实例
2011-05-11 17:31 3882当向Executor提交批处理任务时,并且希望在它们完成后获得 ... -
CyclicBarrier的使用实例
2011-05-11 15:45 1441CyclicBarrier允许给定数量的线程全部到达关卡点时, ... -
CopyOnWriteArrayList工作原理和实例
2011-05-05 23:43 3346CopyOnWriteArrayList顾名思义,在写入操作时 ... -
Semaphore的介绍和使用实例
2011-04-27 22:32 2772Semaphore可以用来控制能 ... -
FutureTask的使用方法和使用实例
2011-04-27 15:34 13263FutureTask是一种可以取消的异步的计算任务。它的计算是 ... -
CountDownLatch的使用实例
2011-04-26 22:20 8186CountDownLatch CountDownl ... -
Java 并发编程基础-共享对象
2011-04-19 14:48 1410Java 并发编程基础 ... -
从JVM并发看CPU内存指令重排序(Memory Reordering)
2011-04-18 16:17 1448我们都知道,现在的计算机, cpu 在计算的时候 ... -
Java并发编程基础
2011-04-15 14:55 1515Java 并发编程基 ... -
Java多线程基础
2011-04-13 15:52 4708Java 多线程基础 ... 2 ... -
硬件同步原语(CAS)理论
2010-09-24 22:13 6953在Java并发编程中,常常 ... -
java5中使用interrupt()来停止java线程的方法(转)
2010-08-18 23:24 3065在开发java多线程时,如果要停止线程这个问题很头痛吧,不过在 ... -
Java 中的Double Check Lock(转)
2010-07-27 21:13 8044对于多线程编程来说,同步问题是我们需要考虑的最多的问题,同步的 ... -
并发访问的问题解决方案
2010-07-26 18:15 1969目前正在做基于Red 5 的Meeting系统,我们会在Mee ... -
用并发包中的重入锁实现生产消费模型
2010-06-15 00:07 1401传统的生产消费模型,实际上是通过一个条件来调节生产者和消费者线 ... -
ThreadLocal原理(转)
2010-03-24 18:06 2129http://jzhua.iteye.com/blog/517 ... -
(转)Java偏向锁实现原理(Biased Locking)
2010-03-21 22:24 1334http://www.iteye.com/topic/5180 ...
相关推荐
C++ 多线程 PV模拟生产者消费者模型多个实例,总结再总结
使用的生产者和消费者模型具有如下特点: (1)本实验的多个缓冲区不是环形循环的,也不要求按顺序访问。生产者可以把产品放到目前某一个空缓冲区中。 (2)消费者只消费指定生产者的产品。 (3)在测试用例文件中指定...
什么是生产者消费者模型 在 工作中,大家可能会碰到这样一种情况:某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是广义的,可以是类、函数、线程、进程等)。产 生数据的模块,就形象地称为...
主要介绍了Java多线程 生产者消费者模型实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Python中,队列是线程间最常用的交换数据的形式。 Python Queue模块有三种队列及构造函数: 1、Python Queue模块的FIFO队列先进先出。 class Queue.Queue(maxsize) 2、LIFO类似于堆,即先进后出。...
根据网上生产消费模型改写的易于理解(自己)的线程例子。
主要介绍了Python自定义进程池,结合实例分析了Python使用自定义进程池实现的生产者、消费者模型问题,需要的朋友可以参考下
读《深入理解计算机系统》后,参照书中程序,用C代码实现生产者消费者模型,本代码可移植性强,各接口以封装好,并有注释
主要介绍了Python生成器实现简单"生产者消费者"模型代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java基于Lock的生产者消费者模型,结合实例形式分析了java基于锁机制的生产者消费者模型相关实现与使用技巧,需要的朋友可以参考下
生成器定义 在Python中,一边循环一边计算的机制,称为生成器:generator。 为什么要有生成器 ...使用生成器实现简单的生产者消费者模型 1、效果截屏 代码如下: import time def consumer(name): print
#生产者消费者模型 其实服务器集群就是这个模型 # 这里介绍的是非yield方法实现过程 import threading,time import queue q = queue.Queue(maxsize=10) def Producer(anme): # for i in range(10): # q.put('骨头%...
多核编程第6章-实例代码: 包含四个并行模型的实例代码,委托(boss-worker) 对等(peer to peer) 流水线(pipeline) 生产者-消费者(producer-consumer),仅是框架的实例代码不包含异常处理,容错等其他考虑。
通过对两种观点的文献综述,依据R.Nelson(1959)的"需求—成本"论,以及"最优创新时间"模型的分析,以消费者主权和生产者主权为理论根基的技术推动与需求拉动观点,可以归纳为以预期利润为导向。生产者(企业家)在技术创新...
实现生产者与消费者模型 ProcOut为.exe的进程外组件 ProOutTest为调用组件的应用程序 程序用VC++6.0编写,实现生产者消费者模型 ====================================================== ProcOut用Debug编译 ===...
使用点对点(Point To Point)模型 点对点模型特点: 只有一个消费者可以接收到消息 ...实例化类 $obj = new Stdclass(); // 3.获取数据 for($i=0; $i<3>username = 'test'; $obj->password = '123456'; $queneNam