前言

小伙伴a,b,c围着吃火锅,当菜上齐了,请客的主人说:开吃!,于是小伙伴一起动筷子,这种场景如何实现

Event(事件)

Event(事件):事件处理的机制:全局定义了一个内置标志Flag,如果Flag值为 False,那么当程序执行 event.wait方法时就会阻塞,如果Flag值为True,那么event.wait 方法时便不再阻塞。

Event其实就是一个简化版的 Condition。Event没有锁,无法使线程进入同步阻塞状态。

Event()

  • set(): 将标志设为True,并通知所有处于等待阻塞状态的线程恢复运行状态。

  • clear(): 将标志设为False。

  • wait(timeout): 如果标志为True将立即返回,否则阻塞线程至等待阻塞状态,等待其他线程调用set()。

  • isSet(): 获取内置标志状态,返回True或False。

Event案例1

场景:小伙伴a和b准备就绪,当收到通知event.set()的时候,会执行a和b线程

# coding:utf-8

import threading
import time event = threading.Event() def chihuoguo(name):
# 等待事件,进入等待阻塞状态
print '%s 已经启动' % threading.currentThread().getName()
print '小伙伴 %s 已经进入就餐状态!'%name
time.sleep(1)
event.wait()
# 收到事件后进入运行状态
print '%s 收到通知了.' % threading.currentThread().getName()
print '小伙伴 %s 开始吃咯!'%name # 设置线程组
threads = [] # 创建新线程
thread1 = threading.Thread(target=chihuoguo, args=("a", ))
thread2 = threading.Thread(target=chihuoguo, args=("b", )) # 添加到线程组
threads.append(thread1)
threads.append(thread2) # 开启线程
for thread in threads:
thread.start() time.sleep(0.1)
# 发送事件通知
print '主线程通知小伙伴开吃咯!'
event.set()

运行结果:

Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
主线程通知小伙伴开吃咯!
Thread-1 收到通知了.
小伙伴 a 开始吃咯!
Thread-2 收到通知了.
小伙伴 b 开始吃咯!

Event案例2

场景:当小伙伴a,b,c集结完毕后,请客的人发话:开吃咯!

# coding:utf-8

import threading
import time event = threading.Event() def chiHuoGuo(name):
# 等待事件,进入等待阻塞状态
print '%s 已经启动' % threading.currentThread().getName()
print '小伙伴 %s 已经进入就餐状态!'%name
time.sleep(1)
event.wait()
# 收到事件后进入运行状态
print '%s 收到通知了.' % threading.currentThread().getName()
print '%s 小伙伴 %s 开始吃咯!'%(time.time(), name) class myThread (threading.Thread): # 继承父类threading.Thread
def __init__(self, name):
'''重写threading.Thread初始化内容'''
threading.Thread.__init__(self) self.people = name def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
'''重写run方法''' chiHuoGuo(self.people) # 执行任务
print("qq交流群:226296743")
print("结束线程: %s" % threading.currentThread().getName()) # 设置线程组
threads = []
# 创建新线程
thread1 = myThread("a")
thread2 = myThread("b")
thread3 = myThread("c") # 添加到线程组
threads.append(thread1)
threads.append(thread2)
threads.append(thread3) # 开启线程
for thread in threads:
thread.start() time.sleep(0.1)
# 发送事件通知
print '集合完毕,人员到齐了,开吃咯!'
event.set()

运行结果:

Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
Thread-3 已经启动
小伙伴 c 已经进入就餐状态!
集合完毕,人员到齐了,开吃咯!
Thread-1 收到通知了.
1516780957.47 小伙伴 a 开始吃咯!
qq交流群:226296743
结束线程: Thread-1
Thread-3 收到通知了.
1516780957.47 小伙伴 c 开始吃咯!Thread-2 收到通知了.
qq交流群:226296743 1516780957.47 小伙伴 b 开始吃咯!结束线程: Thread-3 qq交流群:226296743
结束线程: Thread-2

python自动化交流 QQ群:779429633

最新文章

  1. STM32环境搭建/学习观点/自学方法 入门必看
  2. zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle1
  3. XML转换为Map通用算法实现 Java版本(Stax实现)
  4. Codeforces Beta Round #51 B. Smallest number dfs
  5. C/C++代码检视要点
  6. Sequence《优先队列》
  7. Robotium API -- click/clickLong操作
  8. USB VID PID 查询
  9. JavaScript贪食蛇游戏制作详解
  10. MyCAT-EYE开源
  11. iOS 极光推送的集成以及一些集成后的狗血
  12. web项目错误页面友好处理404,500等
  13. java 零基础搭建dubbo运行环境
  14. 基于python的种子搜索网站,你懂得!
  15. python requests上传文件 tornado 接收文件
  16. 基于asp.net的excel导入导出
  17. 基于tensorflow的逻辑分类
  18. CentOS7 上学习使用docker
  19. 软件魔方制作系统启动盘并安装win10系统
  20. OpenCV学习(27) 直方图(4)

热门文章

  1. C后端设计开发 - 第2章-内功-数据结构上卷
  2. nodejs面试题
  3. shell常见操作整理(更新)
  4. 关于ueditor在Java中文件上传问题,404问题
  5. [译]怎样用HTML5 Canvas制作一个简单的游戏
  6. hdu 2883(构图+最大流+压缩区间)
  7. Graph Cut 简介
  8. .net/c#常用框架/中间件简介(不定时更新)
  9. OpenFalcon-SuitAgent
  10. RedHat7/Centos7 搭建NFS服务器