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的更多相关文章

  1. Thread Based Parallelism - Thread Synchronization With Lock

    Thread Based Parallelism - Thread Synchronization With Lock import threading shared_resource_with_lo ...

  2. Thread Based Parallelism - Thread in a Subclass

    Thread Based Parallelism - Thread in a Subclass 1 import threading import time exit_Flag = 0 class m ...

  3. 【转】Native Thread for Win32 B-Threads Synchronization(通俗易懂,非常好)

    http://www.bogotobogo.com/cplusplus/multithreading_win32B.php   Synchronization Between Threads In t ...

  4. Thread in depth 3:Synchronization

    Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...

  5. CyclicBarrier、CountDownLatch、Callable、FutureTask、thread.join() 、wait()、notify()、Condition

    CyclicBarrier使用: import java.util.Random; import java.util.concurrent.BrokenBarrierException; import ...

  6. 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别

    JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...

  7. Thread.sleep( ) vs Thread.yield( )

    Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...

  8. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...

  9. PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别

    链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...

随机推荐

  1. css写斜角

    项目开发中遇到了这样的效果,百度了一波,可以使用css3的伪类实现: /*斜角公用*/1.外层的div加class='wrapper' 并需要设置相对定位 .wrapper:before { -moz ...

  2. .NET 在云原生时代的蜕变,让我在云时代脱颖而出

    .NET 生态系统是一个不断变化的生态圈,我相信它正在朝着一个伟大的方向发展.有了开源和跨平台这两个关键优先事项,我们就可以放心了.云原生对应用运行时的不同需求,说明一个.NET Core 在云原生时 ...

  3. 【开源】后台权限管理系统升级到aspnetcore3.1

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  4. Mybatis中jdbcType的类型

    具体支持的类型参见:org.apache.ibatis.type.JdbcType ARRAY, BIT, TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, REA ...

  5. 2018南京现场赛K 随机输出

    题目链接:http://codeforces.com/gym/101981/attachments n和m太小,空地联通无环,总步数太大,直接随机输出5w个方向 #include<iostrea ...

  6. java项目Jenkins部署

    假设背景: Nginx跳板机服务器:192.168.10.1 Tomcat应用服务器:192.168.10.3 端口:10083 应用名称:appXXX 1.配置跳板机的转发路径 如:192.168. ...

  7. 《爬虫学习》(二)(urllib库使用)

    urllib库是Python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. 1.urlopen函数: 在Python3的urllib库中,所有 ...

  8. 开发环境Vue访问后端接口教程(前后端分离开发,端口不同下跨域访问)

    原理:开发环境下的跨域:在node.js上实现请求转发,vue前端通过axios请求到node.js上,node.js将请求转发到后端,反之.响应也是,先到node.js上,然后转发vue-cil项目 ...

  9. Windows版Redis主从配置

    一.下载 从github上下载Redis的zip包,地址:https://github.com/MicrosoftArchive/redis/releases Redis本身不支持windows,这是 ...

  10. OpenCV里的颜色空间

    RGB三原色组合方式是最常用的 RGB色彩空间: R:红色分量 G:绿色分量 B:蓝色分量 HSV色彩空间: H - 色调(主波长). S - 饱和度(纯度/色调). V - 明度(强度). LAB色 ...