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. AspectJ——预编译方式实现AOP

  2. Java BIO NIO 与 AIO

    回顾 上一章我们介绍了操作系统层面的 IO 模型. 阻塞 IO 模型. 非阻塞 IO 模型. IO 复用模型. 信号驱动 IO 模型(用的不多,知道个概念就行). 异步 IO 模型. 并且介绍了 IO ...

  3. APICloud发布低代码开发平台

    云原生的出现,致使传统IT模式正在集中向云架构.云开发转型,其中在企业业务的互联网化.数字化进程中尤为突出,并衍生出“敏捷开发”.“快速迭代”的刚性需求.面对双模IT,如何打造全新的IT团队与模式?并 ...

  4. 暑假提高组集训Day1 T2

    那么这一道题我在考试的时候写挂了(0分 呜呜~) 我原来的思路是广搜来骗取部分分(哈哈~) 但是我忘记了一个非常重要的问题 我广搜开的数组没有考虑负的下标 下一次考试如果再写暴力 就可以把坐标都加上一 ...

  5. dfs - 走过的标记取消

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...

  6. python认识及环境变量

    什么是python? python是一种脚本语言,是高级语言.计算机只能识别机器语言,在机器语言上是汇编语言,再往上是高级语言.高级语言的基础是C语言. python语言较为简单,易入门. pytho ...

  7. webpack进阶用法你都get到了么?

    如何消除无用代码:打包自己的私有js库:实现代码分割和动态import提升初次加载速度:配置eslint规范团队代码规范:打包异常抓捕你都get到了么? 摇树优化:Tree Shaking webpa ...

  8. Kaggle竞赛丨入门手写数字识别之KNN、CNN、降维

    引言 这段时间来,看了西瓜书.蓝皮书,各种机器学习算法都有所了解,但在实践方面却缺乏相应的锻炼.于是我决定通过Kaggle这个平台来提升一下自己的应用能力,培养自己的数据分析能力. 我个人的计划是先从 ...

  9. @ControllerAdvice自定义异常统一处理

    正常来说一个系统肯定有很多业务异常.而这些业务异常的信息如何返回给前台呈现给用户.比如用户的某些操作不被允许,需要给用户提示. Spring 提供了@ControllerAdvice这个注解,这个注解 ...

  10. 如何查看Ubuntu服务器上Django项目的MySQL服务有没有挂?

    如果你在项目内输入:MySQL -uroot -p 接着输入密码之后返回ERROR:1045……如下图 那么这就说明MySQL服务挂了. 需要重新启动一下 重启时在项目目录下输入:sudo servi ...