`
zengshaotao
  • 浏览: 777856 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

生产者消费者进阶(带有condition)

 
阅读更多

package function.thread;

 

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Random;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**

功能:三个生产者,往容量最大为5的容器里put数据

三个消费者从容器中取数据。只要容器不为空,就可以取数据,

只要容器不满,就可以存放数据

容器里不能拥有相同的数据

*/

public class ConditionTest {

 

public static void main(String args[]) {

 

final String goonFlag[] = {"true"};

final BoundedBuffer bf = new BoundedBuffer();

System.out.println("**************************************** main thread begin********************************");

//刚开始是抢占式的,take和put线程都可能先执行

for (int i = 0; i < 3; i++) {

new Thread(" put thread "+i) {

public void run() {

try {

//这里是匿名的内部类,所以要使用final变量

while(goonFlag[0].equals("true")){

bf.put(new Random().nextInt(100));

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}.start();

 

new Thread(" take thread "+i) {

public void run() {

try {

while(goonFlag[0].equals("true")){

try {

bf.take();

} catch (Exception e) {

e.printStackTrace();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}.start();

}

 

try {

Thread.sleep(30000);

goonFlag[0]= "false";

} catch (InterruptedException e) {

e.printStackTrace();

}

}//main method

}

 

class BoundedBuffer {

final Lock lock = new ReentrantLock();// 锁对象

final Condition putCond = lock.newCondition();// 队列未满,写线程可以满足执行条件

final Condition takeCond = lock.newCondition();// 读线程条件

 

final List items = new ArrayList();// 缓存队列

 

public void put(Object x) throws InterruptedException {

lock.lock();

String threadName = Thread.currentThread().getName();

System.out.println("############### 【"+threadName+"】 get lock and sleep 500 ms ");

Thread.sleep(500);

try {

if (items.size()>=5){

System.out.println("############### 【"+threadName+"】 stack is full and await ");

takeCond.signalAll();//队列已经满了,读线程可以进行读取了

// 阻塞写线程

//一个线程被阻塞,从业务角度理解,它的使命已经完成

//如果要重新参与作业,就要重新参与线程的竞争

putCond.await();

}else{

if(!items.contains(x)){

items.add(x);// 赋值

System.out.println("############### 【"+threadName+"】 put: "+x);

System.out.println("############### 【"+threadName+"】 list length "+items.size()+", list content:"+items.toString());

takeCond.signalAll();//队列不为空,读线程可以进行读取了

}else{

System.out.println("############### 【"+threadName+"】 put same: "+x);

System.out.println("############### 【"+threadName+"】 list content:"+items.toString());

}

 

}

 

} finally {

System.out.println("############### 【"+threadName+"】 release the lock ");

lock.unlock();

}

}

 

public Object take() throws Exception {

lock.lock();

String threadName = Thread.currentThread().getName();

//int listSize = items.size();不能使用局部变量,因为如果使用,得到的可能是await之前的值

//await期间,该值可能已经被修改

System.out.println("**************** 【"+threadName+"】 get a lock and sleep 500ms ");

Thread.sleep(500);

try {

if (items.size() == 0){

System.out.println("**************** 【"+threadName+"】 stack is empty and await ");

putCond.signalAll();// 队列为空,就可以继续写入,也就是可以唤醒写线程

//当前线程调用了condition,被打上了相应的标签

//阻塞读线程。这里需要注意,被贴上了参与共享资源标签的线程在被唤醒后,

//可能会和还未打上标签的线程竞争。还有一种就是被打上了标签之后的多个线程同时竞争

takeCond.await();

return null;

}else{

Object x = items.get(items.size()-1);// 取值

System.out.println("**************** 【"+threadName+"】 get the value: "+x);

items.remove(x);

System.out.println("**************** 【"+threadName+"】 remain list : "+items.toString());

putCond.signalAll();// 队列未满,就可以继续写入,也就是可以唤醒写线程

return x;

}

 

 

}catch(Exception e){

e.printStackTrace();

return null;

}finally {

System.out.println("**************** 【"+threadName+"】 release the lock ");

lock.unlock();

}

}

}

 

运行结果:

**************************************** main thread begin********************************

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 66

############### 【 put thread 0】 list length 1, list content:[66]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 66

**************** 【 take thread 0】 remain list : []

**************** 【 take thread 0】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 57

############### 【 put thread 1】 list length 1, list content:[57]

############### 【 put thread 1】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 94

############### 【 put thread 1】 list length 2, list content:[57, 94]

############### 【 put thread 1】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 90

############### 【 put thread 2】 list length 3, list content:[57, 94, 90]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 23

############### 【 put thread 2】 list length 4, list content:[57, 94, 90, 23]

############### 【 put thread 2】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 23

**************** 【 take thread 1】 remain list : [57, 94, 90]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 get the value: 90

**************** 【 take thread 2】 remain list : [57, 94]

**************** 【 take thread 2】 release the lock 

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 77

############### 【 put thread 0】 list length 3, list content:[57, 94, 77]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 77

**************** 【 take thread 0】 remain list : [57, 94]

**************** 【 take thread 0】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 74

############### 【 put thread 1】 list length 3, list content:[57, 94, 74]

############### 【 put thread 1】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put same: 94

############### 【 put thread 2】 list content:[57, 94, 74]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 28

############### 【 put thread 2】 list length 4, list content:[57, 94, 74, 28]

############### 【 put thread 2】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 28

**************** 【 take thread 1】 remain list : [57, 94, 74]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 74

**************** 【 take thread 1】 remain list : [57, 94]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 get the value: 94

**************** 【 take thread 2】 remain list : [57]

**************** 【 take thread 2】 release the lock 

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 15

############### 【 put thread 0】 list length 2, list content:[57, 15]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 15

**************** 【 take thread 0】 remain list : [57]

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 57

**************** 【 take thread 0】 remain list : []

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 stack is empty and await 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 78

############### 【 put thread 1】 list length 1, list content:[78]

############### 【 put thread 1】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 70

############### 【 put thread 2】 list length 2, list content:[78, 70]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 40

############### 【 put thread 2】 list length 3, list content:[78, 70, 40]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 49

############### 【 put thread 2】 list length 4, list content:[78, 70, 40, 49]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 60

############### 【 put thread 2】 list length 5, list content:[78, 70, 40, 49, 60]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 stack is full and await 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 60

**************** 【 take thread 1】 remain list : [78, 70, 40, 49]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 49

**************** 【 take thread 1】 remain list : [78, 70, 40]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 get the value: 40

**************** 【 take thread 2】 remain list : [78, 70]

**************** 【 take thread 2】 release the lock 

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 31

############### 【 put thread 0】 list length 3, list content:[78, 70, 31]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 31

**************** 【 take thread 0】 remain list : [78, 70]

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 70

**************** 【 take thread 0】 remain list : [78]

**************** 【 take thread 0】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 23

############### 【 put thread 1】 list length 2, list content:[78, 23]

############### 【 put thread 1】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 91

############### 【 put thread 1】 list length 3, list content:[78, 23, 91]

############### 【 put thread 1】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 57

############### 【 put thread 1】 list length 4, list content:[78, 23, 91, 57]

############### 【 put thread 1】 release the lock 

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 22

############### 【 put thread 2】 list length 5, list content:[78, 23, 91, 57, 22]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 stack is full and await 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 22

**************** 【 take thread 1】 remain list : [78, 23, 91, 57]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 get the value: 57

**************** 【 take thread 2】 remain list : [78, 23, 91]

**************** 【 take thread 2】 release the lock 

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 20

############### 【 put thread 0】 list length 4, list content:[78, 23, 91, 20]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 20

**************** 【 take thread 0】 remain list : [78, 23, 91]

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 91

**************** 【 take thread 0】 remain list : [78, 23]

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 23

**************** 【 take thread 0】 remain list : [78]

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 78

**************** 【 take thread 0】 remain list : []

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 stack is empty and await 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 77

############### 【 put thread 1】 list length 1, list content:[77]

############### 【 put thread 1】 release the lock 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 5

############### 【 put thread 1】 list length 2, list content:[77, 5]

############### 【 put thread 1】 release the lock 

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 57

############### 【 put thread 2】 list length 3, list content:[77, 5, 57]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 27

############### 【 put thread 2】 list length 4, list content:[77, 5, 57, 27]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 put: 32

############### 【 put thread 2】 list length 5, list content:[77, 5, 57, 27, 32]

############### 【 put thread 2】 release the lock 

############### 【 put thread 2】 get lock and sleep 500 ms 

############### 【 put thread 2】 stack is full and await 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 32

**************** 【 take thread 1】 remain list : [77, 5, 57, 27]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 27

**************** 【 take thread 1】 remain list : [77, 5, 57]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 57

**************** 【 take thread 1】 remain list : [77, 5]

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 get the value: 5

**************** 【 take thread 2】 remain list : [77]

**************** 【 take thread 2】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 get the value: 77

**************** 【 take thread 2】 remain list : []

**************** 【 take thread 2】 release the lock 

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 28

############### 【 put thread 0】 list length 1, list content:[28]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 get the value: 28

**************** 【 take thread 0】 remain list : []

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 0】 get a lock and sleep 500ms 

**************** 【 take thread 0】 stack is empty and await 

############### 【 put thread 1】 get lock and sleep 500 ms 

############### 【 put thread 1】 put: 92

############### 【 put thread 1】 list length 1, list content:[92]

############### 【 put thread 1】 release the lock 

############### 【 put thread 2】 release the lock 

**************** 【 take thread 1】 get a lock and sleep 500ms 

**************** 【 take thread 1】 get the value: 92

**************** 【 take thread 1】 remain list : []

**************** 【 take thread 1】 release the lock 

**************** 【 take thread 2】 get a lock and sleep 500ms 

**************** 【 take thread 2】 stack is empty and await 

############### 【 put thread 0】 get lock and sleep 500 ms 

############### 【 put thread 0】 put: 20

############### 【 put thread 0】 list length 1, list content:[20]

############### 【 put thread 0】 release the lock 

**************** 【 take thread 0】 release the lock 

**************** 【 take thread 2】 release the lock 

 

分享到:
评论

相关推荐

    生产者 消费者 模式 c++

    在C++中实现生产者消费者模式,我们可以利用C++11及更高版本提供的线程库()、互斥量()、条件变量(&lt;condition_variable&gt;)等工具。 生产者消费者模式的基本思想是将数据的生产与消费分离,生产者负责创建数据,...

    labview 生产者消费者例子

    在 LabVIEW 中,“生产者-消费者”模型是一种常见的多线程编程模式,用于解决数据处理中的同步问题。这个模型的核心思想是将数据的生产和消费分隔开,避免了数据处理过程中的冲突和等待,提高了程序执行效率。 生产...

    生产者消费者问题c++实现

    生产者消费者问题是多线程编程中的一个经典案例,它展示了如何通过线程间的协作来解决资源的同步和异步操作。在C++中,我们可以利用标准库中的互斥量(mutex)、条件变量(condition_variable)等工具来实现这个问题...

    多线程简易实现生产者消费者模式

    3. **条件变量(Condition)**:用于线程间通信,如生产者等待消费者消费完产品后才能继续生产,消费者等待产品出现后才能开始消费。Java中,`java.util.concurrent.locks.Condition`接口提供了条件变量功能。 实现...

    python实现生产者消费者并发模型

    多线程实现生产者消费者模型:锁(Lock)、信号量(Semaphore、BoundedSemaphore)、条件(Condition)、队列(Queue)、事件(Event) 多进程程实现生产者消费者模型:信号量(Semaphore)、条件(Condition)、...

    pv.rar_pv_pv操作_生产者消费者_生产者消费者问题_生产者消费者问题 c

    此问题描述的是一个系统中有两个角色:生产者和消费者,生产者负责生成数据,而消费者负责消费这些数据。为了保证系统的稳定和效率,需要避免以下两种情况:一是缓冲区满时生产者继续生产数据,二是缓冲区空时消费者...

    还讲生产者与消费者(condition)

    标题中的“生产者与消费者(condition)”是指在多线程编程中的一个经典问题,它涉及到如何高效地协调生产数据和消费数据的线程。在计算机科学中,生产者是生成数据的线程,而消费者则是使用这些数据的线程。这个问题...

    生产者消费者演示程序

    要解决生产者消费者问题,我们需要用到线程同步机制,比如互斥锁(Mutex)、条件变量(Condition Variable)或者信号量(Semaphore)。这些机制可以防止多个线程同时访问队列,确保数据的正确添加和移除。例如,当...

    编程实现生产者消费者或读写者的同步问题

    在该模型中,我们有两个类型的线程:生产者线程负责生成数据,而消费者线程负责消耗这些数据。关键在于确保生产者不会在消费者未准备好接收数据时生产过多的数据,同时消费者也不会在没有数据可供消费时浪费资源。这...

    jchc.rar_tearshmj_生产者_生产者-消费者问题 c++ _生产者和消费者_生产者消费者

    《生产者-消费者问题在C++中的实现》 生产者-消费者问题是多线程编程中的经典案例,它展示了如何在并发环境中实现线程间的同步与互斥。在本项目中,我们将探讨这个问题,并以C++语言为基础,创建一个在Windows 2000...

    实验一 生产者消费者问题

    生产者消费者问题是多线程编程中的经典模型,用于展示如何在并发环境中协调生产者和消费者之间的数据处理。在这个问题中,生产者线程负责生成数据,而消费者线程则负责消费这些数据。为了保证系统的稳定性和正确性,...

    操作系统实验:生产者消费者

    操作系统中的“生产者消费者”问题是一个经典的多线程同步问题,源自计算机科学的并发控制理论。这个模型描述了两个或多个线程之间的交互,其中一部分线程(生产者)负责生成数据,另一部分线程(消费者)则负责处理...

    生产者消费者问题 MFC

    3. **事件对象**:`CEvent`类可以用来通知生产者何时可以生产新的产品,以及通知消费者何时有产品可消费。例如,可以创建一个空闲事件(EmptyEvent),当缓冲区为空时,生产者会等待这个事件;另一个是满事件...

    操作系统 生产者消费者问题实验代码和报告

    在生产者-消费者问题中,生产者可能需要等待缓冲区有空位,而消费者可能需要等待缓冲区有已填满的位。通过条件变量,线程可以在不消耗CPU资源的情况下等待条件满足。 实验报告通常包括以下部分: - **问题背景**:...

    多进程同步-生产者消费者模式-C实现

    使用条件变量(Condition Variable)来同步生产者和消费者的动作,确保消费者只在缓冲区非空时取数据,生产者在缓冲区非满时生产数据。 在"IPC-model"这个压缩包中,可能包含的代码示例会展示如何定义和初始化这些...

    java多线程实现生产者和消费者

    生产者和消费者线程都需要有自己的业务逻辑,这可以通过重写`run()`方法实现。 2. **共享资源**:在生产者-消费者模型中,通常会有一个固定大小的缓冲区作为共享资源。可以使用数组或链表实现,但需要注意的是,当...

    java模拟生产者和消费者问题

    在Java中,可以通过多种方式实现生产者-消费者模型,包括但不限于使用`wait()`和`notify()`方法、`BlockingQueue`接口、`Semaphore`类以及`ReentrantLock`和`Condition`组合等。 #### 使用`wait()`和`notify()` 这...

    p&c.rar_生产者 消费者_生产者与消费者问题_生产者消费者_生产者消费者问题_生产者消费者问题 c

    在C++中实现生产者-消费者问题,通常会用到线程同步机制,如互斥量(mutex)、条件变量(condition_variable)等。`p&c.cpp`文件可能就是这样一个实现示例,它通过控制并发访问来防止数据竞争和死锁。 1. **互斥量*...

    java生产者消费者demo

    在Java中,有几种常见的解决生产者消费者问题的方法: 1. **阻塞队列(BlockingQueue)**:Java并发包`java.util.concurrent`中的`BlockingQueue`是一个理想的选择。生产者可以使用`put()`方法将产品放入队列,而...

Global site tag (gtag.js) - Google Analytics