python线程condition条件锁应用实例
import time
import threading
# 吃火锅鱼丸
guo = []
suo = threading.Condition() #条件锁
# 生产者负责生产
class Produce(threading.Thread):
def __init__(self):
super().__init__()
def run(self):
suo.acquire() #先锁住
while True:
time.sleep(1)
guo.append("鱼丸")
print("%s生产了一个鱼丸,现在锅里有%s个鱼丸"%(self.name,len(guo)))
if len(guo)>=5:
print("锅里已经有%s个鱼丸"%len(guo))
suo.notify() # 通知
suo.wait() # wait等待 释放另一个线程 # 消费者负责消费
class Consumer(threading.Thread):
def __init__(self):
super().__init__()
def run(self):
suo.acquire()
while True:
time.sleep(1.5)
guo.pop()
print("%s吃了一个鱼丸,现在锅里有%s个鱼丸"%(self.name,len(guo)))
if len(guo)<1:
print("锅里已经没有鱼丸了,请添加鱼丸。")
suo.notify() # 通知
suo.wait()# wait 等待 释放另一个线程
pro = Produce()
pro.start()
con = Consumer()
con.start() # 汽车订单变化
Car = ["保时捷911","丰田普拉多","哈弗H5","奔驰G500","路虎","法拉利","宝马X5","奥迪A8","膜拜单车","大巴车"]
suo = threading.Condition() #条件锁
List = []
index = -1
class car(threading.Thread):
def __init__(self):
super().__init__()
def run(self):
suo.acquire()
while True:
global index
time.sleep(2)
index+=1
List.append(Car[index])
print("恭喜靳志龙喜提一辆%s,请继续选择你喜爱的车"%Car[index])
suo.notify()
if len(List)>=10:
print("对不起,你的银行卡余额不足")
suo.release()
break
else:
suo.wait()
class carbox(threading.Thread):
def __init__(self):
super().__init__()
def run(self):
suo.acquire()
while True:
global index
time.sleep(0.5)
print("仓库里还有%s辆车"%(len(Car)-index-1))
suo.notify()
if len(List) >= 10:
print("哈哈,其实我们也没有车了,小哥哥")
suo.release()
break
else:
suo.wait()
aa = car()
aa.start()
bb = carbox()
bb.start()
python线程condition条件锁应用实例的更多相关文章
- python多线程threading.Lock锁用法实例
本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...
- python线程的条件变量Condition的用法实例
Condition 对象就是条件变量,它总是与某种锁相关联,可以是外部传入的锁或是系统默认创建的锁.当几个条件变量共享一个锁时,你就应该自己传入一个锁.这个锁不需要你操心,Condition 类会 ...
- python多线程--Condition(条件对象)
Condition class threading.Condition(lock=None 这个类实现条件变量对象.条件变量允许一个或多个线程等待,知道它们被另一个线程唤醒. 如果给出了lock参数而 ...
- python 线程池和锁
一.死锁现象与递归锁 锁:Lock线程安全,多线程操作时,内部会让所有线程排队处理.如:list/dict/Queue 线程不安全 + 人 => 排队处理. import thre ...
- (15)线程---Condition条件
功能:也是通过阻塞控制线程数量.类似信号量\进程池\线程池的作用 语法:wait from threading import Condition con= Condition() conn.acq ...
- Linux 开发之线程条件锁那些事
2019独角兽企业重金招聘Python工程师标准>>> 条件锁即在一定条件下触发,那什么时候适合用条件锁呢,那当然是你在等待一个符合的条件下触发.一个常用的例子就是在线程中无限循环执 ...
- python线程条件变量Condition(31)
对于线程与线程之间的交互我们在前面的文章已经介绍了 python 互斥锁Lock / python事件Event , 今天继续介绍一种线程交互方式 – 线程条件变量Condition. 一.线程条件变 ...
- threading 官方 线程对象和锁对象以及条件对象condition
官方地址:https://docs.python.org/2/library/threading.html#thread-objects 以下只截取condition部分,其他Lock()以及thre ...
- 线程高级篇-Lock锁和Condition条件
浅谈Synchronized: synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其 ...
随机推荐
- Logistic回归计算过程的推导
https://blog.csdn.net/ligang_csdn/article/details/53838743 https://blog.csdn.net/weixin_30014549/art ...
- 博客系统(cnblog)
1.用户表:Userinfo 2.博客站点表:Blog 3.标签表:Tag 4.分类表:Category 5.文章表:Article 6.点赞踩表:ArticelUpDown 7.评论表:Commen ...
- idea常用操作大全
1.智能提示忽略大小写 Ctrl+Alt+s打开setting setting-->Editor-->General-->Code Completion 或者直接搜索Code Com ...
- EntityFramwork 七七八八
Tip 技术的选型受技术先进性.技术持续性.技术普及度.推行力度的影响. 我也很无奈,一大把年纪了又要重新学一种ORMapping框架. 说实话,这是我用过的最复杂的ORMapping框架了. Ent ...
- ajax 防止重复提交
参考链接:http://www.hollischuang.com/archives/931 http://blog.csdn.net/everything1209/article/details/52 ...
- 浅谈USB驱动架构 转载
去年,老师让我分析基于HD3系统芯片的WindowsCE USB驱动的可行性.USB驱动非常庞大,多个软件层次相互交错,以及各种协议,USB系统对于一般人很难理解,我对其也只是理解一个大概,下面,我对 ...
- SynchronousQueue------TransferStack源码分析
s,e在线程栈里面,TransferStack在堆里面,方法只是线程的执行逻辑.线程过来调用transfer方法,线程在堆里面创建一个节点,加到Stack里面去,然后这个线程归属节点的waiter,阻 ...
- ARMLCD屏调试2--ili9325驱动编写
2011-06-24 21:57:09 一,读id--完成 二,自己写了驱动--完成 三,调试LCD屏驱动--完成 一 读id,不过过程还有点曲折. 因为之前写命令写数据的时序是根据i ...
- 对oracle数据库的数据迁移
导入:expdp 用户名/密码@ip/实例名 DUMPFILE=dump.dump EXCLUDE=TABLE:\"IN (\'表1\',\'表2\')\" 导出:impdp 用户 ...
- 58 字体反爬攻略 python3
1.下载安装包 pip install fontTools 2.下载查看工具FontCreator 百度后一路傻瓜式安装即可 3.反爬虫机制 网页上看见的 后台源代码里面的 从上面可以看出,生这个字变 ...