Thread Based Parallelism - Thread Synchronization With a Condition
Thread Based Parallelism - Thread Synchronization With a Condition
from threading import Thread, Condition
import time items = []
condition = Condition() class consumer(Thread):
def __init__(self):
Thread.__init__(self) def consume(self):
global condition
global items condition.acquire()
if len(items) == 0:
condition.wait()
print("Consumer notify : no item to consume")
items.pop()
print("Consumer notify : consumed 1 item")
print("Consumer notify : items to consume are : " + str(len(items)))
if len(items) == 0:
print("Consumer notify : no items to consume in future")
condition.notify()
condition.release() def run(self):
for i in range(0, 10):
time.sleep(2)
self.consume() class producer(Thread):
def __init__(self):
Thread.__init__(self) def produce(self):
global condition
global items condition.acquire()
if len(items) == 4:
condition.wait()
print("Producer notify : items producted are " + str(len(items)))
print("Producer notify : stop the production!!")
items.append(1)
print("Producer notify : total items producted " + str(len(items)))
condition.notify()
condition.release() def run(self):
for i in range(0, 10):
time.sleep(1)
self.produce() if __name__ == "__main__":
producer = producer()
consumer = consumer() producer.start()
consumer.start() producer.join()
consumer.join() Output,
Producer notify : total items producted 1
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 0
Consumer notify : no items to consume in future
Producer notify : total items producted 1
Producer notify : total items producted 2
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 1
Producer notify : total items producted 2
Producer notify : total items producted 3
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 2
Producer notify : total items producted 3
Producer notify : total items producted 4
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 3
Producer notify : total items producted 4
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 3
Producer notify : items producted are 3
Producer notify : stop the production!!
Producer notify : total items producted 4
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 3
Producer notify : items producted are 3
Producer notify : stop the production!!
Producer notify : total items producted 4
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 3
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 2
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 1
Consumer notify : consumed 1 item
Consumer notify : items to consume are : 0
Consumer notify : no items to consume in future
Thread Based Parallelism - Thread Synchronization With a Condition的更多相关文章
- Thread Based Parallelism - Thread Synchronization With Lock
Thread Based Parallelism - Thread Synchronization With Lock import threading shared_resource_with_lo ...
- Thread Based Parallelism - Thread in a Subclass
Thread Based Parallelism - Thread in a Subclass 1 import threading import time exit_Flag = 0 class m ...
- 【转】Native Thread for Win32 B-Threads Synchronization(通俗易懂,非常好)
http://www.bogotobogo.com/cplusplus/multithreading_win32B.php Synchronization Between Threads In t ...
- Thread in depth 3:Synchronization
Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...
- CyclicBarrier、CountDownLatch、Callable、FutureTask、thread.join() 、wait()、notify()、Condition
CyclicBarrier使用: import java.util.Random; import java.util.concurrent.BrokenBarrierException; import ...
- 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别
JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...
- Thread.sleep( ) vs Thread.yield( )
Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...
- Thread系列之Thread.Sleep(0)
线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...
- PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...
随机推荐
- szTom's Code Style
介绍szTom在C++中使用的代码风格. 头文件 必须使用using namespace std; 如果是C头文件,必须使用c前缀文件名. #include <cstdio> 而不是 #i ...
- 对网站和项目使用IIS Express的64位版
- .NET绘制旋转太极图
.NET绘制旋转太极图 我之前发了一篇<用.NET写"算命"程序>的文章,但有人纷纷提出了质疑,认为没有"科学"(mi xin)依据
- C++数值计算
1.序 (1)程序设计分两种: 1.结构化设计(面向过程)——分解算法为模块,将算法的步骤分解为模块. 2.面向对象程序设计——主要是“类”与“对象”. (2)进制的转换 1.二进制转十进制 整数部分 ...
- [新详细]让Keil5续签到2032年的办法,不可商用
# 使用方法和以前的版本一样,MDK 或者C51等均适用,供学习与参考.更多需要到这里购买→ → Keil官网:[http://www.keil.com/](http://www.keil.com/) ...
- GP工作室—团队项目总结
GP工作室-团队项目总结 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求在哪里 ...
- generic
是什么 算法实现时保有待定类型的参数. 为什么 一份代码用于多个算法(当算法中只数个类型不同的时候) 可重新性 很多常用算法和容器数据结构都可以type-generic的方式实现 why not 许多 ...
- [转]dll反编译工具(ILSpy)的使用
软件地址: 链接:https://pan.baidu.com/s/1YunJ3MAuNisGtl8YYzr4hw 密码:ejx8 工具使方法 1.将压缩文件进行解压,打开exe文件. 2.打开后,选择 ...
- Basic Thought / Data Structure: 前缀和 Prefix Sum
Intro: 在OI中,前缀和是一种泛用性很高的数据结构,也是非常重要的优化思想 Function: 求静态区间和 模板题:输入序列\(a_{1..n}\),对于每一个输入的二元组\((l,r)\), ...
- docker swoft
docker swoft 安装并运行docker docker run -d -p 80:80 --name swoft swoft/swoft docker ps 查看正在运行的容器 docker ...