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
相关推荐
多线程实现生产者消费者模型:锁(Lock)、信号量(Semaphore、BoundedSemaphore)、条件(Condition)、队列(Queue)、事件(Event) 多进程程实现生产者消费者模型:信号量(Semaphore)、条件(Condition)、...
线程并发控制condition 互斥量 多线程写的:生产者、消费者问题
使用Qt 和 C++11 的std::mutex 和 std::condition_variable 实现一个演示生产者消费者模式的Qt工程。
通过java语言编写的生产者消费者,实现方法为lock类和condition类的配合完成。
利用线程间的通信主要是因为当多个线程同时对一个对象进行访问的时候,多个线程之间是一个协助的关系,举个例子就是今天要说的生产这和消费者模型。
NULL 博文链接:https://xylong.iteye.com/blog/1515006
Java实现生产者消费者模型 生产者消费者模型,是一般面试题都会考的,下面介绍使用ReetrantLock实现 生产者消费者模型。 定义一个ReentrantLock锁,同时new出两个condition,一个控制队满,一个 控制队空 //生产者 ...
主要介绍了Lock、Condition实现简单的生产者消费者模式示例,需要的朋友可以参考下
java多线程实现生产者和消费者 ,4种实现方式,分别为synchronizated,condition和lock,信号量,阻塞队列
如果你对生产者—消费者问题有了解,看这篇博客会更有意义。 为什么要关心生产者—消费者问题: 可以帮你更好地理解并发和不同概念的并发。 信息队列中的实现中,一定程度上使用了生产者—消费者问题的概念,而你...
本文实例讲述了python条件变量之生产者与消费者操作。分享给大家供大家参考,具体如下: 互斥锁是最简单的线程同步机制,面对复杂线程同步问题,Python还提供了Condition对象。Condition被称为条件变量,除了提供与...
比较实用,是操作系统很好的教辅资料。很详细的讲解。
在char01包里放置Java多线程基本知识的代码。内容如下: 如何使用多线程 如何得到多线程的一些信息 如何停止线程 如何暂停线程 线程的一些其他用法 在char02包里放置了Java对变量和对象... 多对多 生产者/消费者
Run Condition Plugin 1.0 hpi
前端项目-condition,Advanced condition library
线程通信(Condition)实例,完整的代码文件,你只需要编译运行即可,就可以看看结果,然后分析。
MyBatisPlus条件构造器Condition的用法示例代码
SQL_CONDITION组成WHERE条件类
通过@Bean和@Condition 注解自定义对于的condition里面根据自定义的条件实现指定类注入到spring中;@ConditionalOnProperty可以根据配置文件中的 属性值不同将不同的类注入到spring中 该资源中案例完整,代码简单移动