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 ...
随机推荐
- Scala与Mongodb实践1-----mongodbCRUD
目的:如何使用MongoDB之前提供有关Scala驱动程序及其异步API. 1.现有条件 IDEA中的:Scala+sbt+SDK mongodb-scala-driver的网址:http://mon ...
- .net 解析嵌套JSON
JSON格式文件如下:我们是要取出msgJsoncontent里面GeneralReportInfo下serviceData中的totalUseValue数据 { ", "mess ...
- Proxmox VE:自建虚拟化方案
Proxmox VE 简介 Proxmox Virtual Environment,或 Proxmox VE,是来自德国的开源虚拟化方案.软件和社区支持都是免费的,企业用户则可以通过订阅制获得付费商业 ...
- C++数值计算
1.序 (1)程序设计分两种: 1.结构化设计(面向过程)——分解算法为模块,将算法的步骤分解为模块. 2.面向对象程序设计——主要是“类”与“对象”. (2)进制的转换 1.二进制转十进制 整数部分 ...
- Burpsuite设置拦截response
一.Burpsuite设置拦截HTTP/HTTPS代理 1, .拦截修改request 首先进入Proxy-Options-Intercept Client Requests设置request拦截的规 ...
- Selenium的简单使用
selenium的使用对于新手来说十分友好,因为他避开了如今网络中的异步加载抓取的困扰,使得我们大部分的时间可以用于提取信息和存储中,下面就简单的列一些使用的代码,希望给同样初学的你有一定的参考价值. ...
- Java8 Stream用法详解
1.概述 Stream 的原理:将要处理的元素看做一种流,流在管道中传输,并且可以在管道的节点上处理,包括过滤筛选.去重.排序.聚合等.元素流在管道中经过中间操作的处理,最后由最终操作得到前面处理的结 ...
- 什么是Druid
一.Druid是什么 Druid 单词来源于西方古罗马的神话人物,中文常常翻译成德鲁伊. 玩过魔兽世界,暗黑破坏神,Dota,炉石传说,Dota自走棋的朋友,对这个词一定不陌生. 本文中所介绍的Dru ...
- Flask接口返回JSON格式数据自动解析
一 自定义一个response类 from flask import Response, jsonify # 定义response返回类,自动解析json class JSONResponse(Res ...
- Springboot | @RequestBody 接收到的参数对象属性为空
背景 今天在调试项目的时候遇到一个坑,用Postman发送一个post请求,在Springboot项目使用@RequestBody接收时参数总是报不存在,但是多次检查postman上的请求格式以及项目 ...