python多线程--theading模块

使用Condition对象可以在某些事件触发或者达到特定的条件后才处理数据,Condition除了具有Lock对象的acquire方法和release方法外,

还有wait方法,notify方法,notifyAll方法等用于条件处理。

条件变量保持线程同步:threading.Condition()

wait():线程挂起,直到收到一个notify通知才会被唤醒继续运行

notify():通知其他线程,那些挂起的线程接到这个通知之后会开始运行

notifyAll(): 如果wait状态线程比较多,notifyAll的作用就是通知所有线程(这个一般用得少)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PS:有朋友说看了我日志里面的代码也看了注释,但是还是不懂,那今天来个简单的,
用一对屌丝(空白哥和西米哥)的对话来诠释代码(生产者-消费者)原图:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#coding:utf-8
import  threading
import  time
cond  =  threading.Condition()
class  kongbaige(threading.Thread):
     def  __init__( self , cond, diaosiname):
         threading.Thread.__init__( self , name  =  diaosiname)
         self .cond  =  cond
           
     def  run( self ):
         self .cond.acquire()  #获取锁
           
         print  self .getName()  +  ':一支穿云箭'   #空白哥说的第一句话
         self .cond.notify()                    #唤醒其他wait状态的线程(通知西米哥 让他说话)
         #然后进入wait线程挂起状态等待notify通知(等西米哥的回复,接下来俩人就开始扯蛋)
         self .cond.wait()
           
         print  self .getName()  +  ':山无棱,天地合,乃敢与君绝!'
         self .cond.notify()
         self .cond.wait()
           
         print  self .getName()  +  ':紫薇!!!!(此处图片省略)'
         self .cond.notify()
         self .cond.wait()
           
         print  self .getName()  +  ':是你'
         self .cond.notify()
         self .cond.wait()
           
         #这里是空白哥说的最后一段话,接下来就没有对白了
         print  self .getName()  +  ':有钱吗 借点'
         self .cond.notify()              #通知西米哥
         self .cond.release()             #释放锁
           
           
           
class  ximige(threading.Thread):
     def  __init__( self , cond, diaosiname):
         threading.Thread.__init__( self , name  =  diaosiname)
         self .cond  =  cond
           
     def  run( self ):
         self .cond.acquire()
         self .cond.wait()    #线程挂起(等西米哥的notify通知)
           
         print  self .getName()  + ':千军万马来相见'
         self .cond.notify()   #说完话了notify空白哥wait的线程
         self .cond.wait()     #线程挂起等待空白哥的notify通知
           
         print  self .getName()  +  ':海可枯,石可烂,激情永不散!'
         self .cond.notify()
         self .cond.wait()
           
         print  self .getName()  +  ':尔康!!!(此处图片省略)'
         self .cond.notify()
         self .cond.wait()
           
         print  self .getName()  +  ':是我'
         self .cond.notify()
         self .cond.wait()
           
         #这里是最后一段话,后面空白哥没接话了 所以说完就释放锁 结束线程
         print  self .getName()  +  ':滚' 
         self .cond.release()
           
           
kongbai  =  kongbaige(cond,  '    ' )
ximi  =  ximige(cond,  '西米' )
#尼玛下面这2个启动标志是关键,虽然是空白哥先开的口,但是不能让他先启动,
#因为他先启动的可能直到发完notify通知了,西米哥才开始启动,
#西米哥启动后会一直处于44行的wait状态,因为空白哥已经发完notify通知了进入wait状态了,
#而西米哥没收到
#造成的结果就是2根线程就一直在那挂起,什么都不干,也不扯蛋了
ximi.start()
kongbai.start()
######运行结果######
     :一支穿云箭
西米:千军万马来相见
     :山无棱,天地合,乃敢与君绝!
西米:海可枯,石可烂,激情永不散!
     :紫薇!!!!(此处图片省略)
西米:尔康!!!(此处图片省略)
     :是你
西米:是我
     :有钱吗 借点
西米:滚

转自:http://zeping.blog.51cto.com/6140112/1258977

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值