python线程通信Condition的实例用法介绍
发表于:2024-10-26 作者:千家信息网编辑
千家信息网最后更新 2024年10月26日,这篇文章主要介绍"python线程通信Condition的实例用法介绍",在日常操作中,相信很多人在python线程通信Condition的实例用法介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好
千家信息网最后更新 2024年10月26日python线程通信Condition的实例用法介绍
这篇文章主要介绍"python线程通信Condition的实例用法介绍",在日常操作中,相信很多人在python线程通信Condition的实例用法介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"python线程通信Condition的实例用法介绍"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1、acquire调用Condition关联的方法。
Lock的acquire()或release()。
2、wait传入timeout参数。
指定该线程最多等待多少秒。
导致当前线程进入Condition的等待池等待通知并释放锁,直到其他线程调用该Condition的notify()或者notify_all()方法来唤醒该线程。在调用该wait()方法时可以
3、notify唤醒Condition的单个线程并通知。
收到通知的线程会自动调用accquire()方法尝试加锁。如果所有线程都在该Condition等待池中等待,则会选择唤醒其中一个线程,选择是任意性的。
4、notify_all唤醒所有线程并通知。
实例
import threadingclass Account: # 定义构造函数 def __init__(self, account_no, balance): self.account_no = account_no self._balance = balance self.condition = threading.Condition() # 定义代表是否已经存钱的标识 self.__deposit_flag = False # 取钱 def draw(self, draw_amount): # 加锁 self.condition.acquire() try: # 还没存钱 if not self.__deposit_flag: self.condition.wait() else: if self._balance >= draw_amount: self._balance = self._balance - draw_amount print(threading.current_thread().getName() + " 取钱成功,账户余额是:" + str(self._balance) + "\n") else: print(threading.current_thread().getName() + " 取钱失败\n") # 将标识账户已有存款的标识改成False self.__deposit_flag = False # 唤醒其他等待现车线程 self.condition.notify_all() finally: # 释放锁 self.condition.release() # 存钱 def deposit(self, deposit_amount): # 加锁 self.condition.acquire() try: # 如果已经存款了,则等待取款 if self.__deposit_flag: self.condition.wait() else: self._balance = self._balance + deposit_amount print(threading.current_thread().getName() + " 存款成功,存款金额是:" + str(deposit_amount) + "\n") # 将存款标识改成已存款 self.__deposit_flag = True # 唤醒其他线程 self.condition.notify_all() finally: # 释放锁 self.condition.release() def draw_many(account, draw_amount, max): for i in range(max): account.draw(draw_amount) def deposit_many(account, deposit_amount, max): for i in range(max): account.deposit(deposit_amount) # 创建一个账户account = Account("账户一", 0)# 创建并启动取钱线程draw_1 = threading.Thread(name='取钱者一', target=draw_many, args=(account, 200, 50))draw_1.start()draw_2 = threading.Thread(name='取钱者二', target=draw_many, args=(account, 200, 50))draw_2.start()# 创建并启动存钱线程deposit_1 = threading.Thread(name='存钱者一', target=deposit_many, args=(account, 200, 50))deposit_1.start()deposit_2 = threading.Thread(name='存钱者二', target=deposit_many, args=(account, 200, 50))deposit_2.start()draw_1.join()draw_2.join()deposit_1.join()deposit_2.join()
到此,关于"python线程通信Condition的实例用法介绍"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
线程
取钱
存款
实例
方法
通信
标识
账户
学习
成功
更多
帮助
选择
实用
接下来
代表
任意性
余额
函数
单个
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京市网络安全建设
学术型网络安全学什么
蛮王柯南服务器管理权
软件开发模式常用术语
网络安全维护什么意思
网络技术大赛队名
徐汇区系统软件开发管理
app软件开发服务类合同模板
网络安全法相关公务员岗位
华为5g 网络技术专题
网络技术市场需求分析
计算机运用技术和网络技术
气功视频软件开发
在电信购买一个服务器
通信网络安全管理做什么
执行定时任务数据库挂
河南安卓软件开发哪家可靠
必应浏览器代理服务器出现问题
博兴微信小程序软件开发价格
Linux服务器被锁定怎么解除
花雨庭服务器版本要求
重装系统怎样备份数据库
网络安全手抄报4k纸四年级的
越做软件开发越累
网络安全法的判断题
盐城通信网络技术服务费
上海猫霸网络技术有限公司
自己写个数据库软件有哪些
ip网络技术发展趋势
战网平台服务器加载不了