forked from andaok/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadCondition.py
More file actions
64 lines (46 loc) · 1.2 KB
/
ThreadCondition.py
File metadata and controls
64 lines (46 loc) · 1.2 KB
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
# -*- encoding:utf-8 -*-
'''
Created on Sep 19, 2012
@author: root
write by wye in clouidya
'''
import threading
import time
#商品
product = None
#条件变量
con = threading.Condition()
#生产者方法
def produce():
global product
print "produce acquire lock...."
if con.acquire():
print "produce get lock..."
while True:
if product is None:
print "produce...."
product = "anything"
#通知消费者,商品已经生产.
con.notify()
#等待通知
con.wait()
time.sleep(2)
#消费者方法
def consume():
global product
print "consume acquie lock..."
if con.acquire():
print "consume get lock..."
while True:
if product is not None:
print "consume..."
product = None
#通知生产者,商品已经没了
con.notify()
#等待通知
con.wait()
time.sleep(2)
t1 = threading.Thread(target=produce)
t2 = threading.Thread(target=consume)
t1.start()
t2.start()