使用条件变量保持线程同步
Python的Condition对象提供了对复杂线程同步的支持。使用Condition对象可以在某些事件触发后才处理数据。
Condition对象除了具有acquire方法和release方法以外,还有wait方法、notify方法、notifyAll方法等用于
条件处理。
实例代码演示如下
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
| # _*_ coding: utf-8 _*_
import threading
class Producer(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self, name=name)
def run(self):
global x
con.acquire()
if x == 100000:
con.wait()
pass
else:
for i in range(100000):
x = x + 1
con.notify()
print(x)
con.release()
class Consumer(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self, name=name)
def run(self):
global x
con.acquire()
if x == 0:
con.wait()
pass
else:
for i in range(100000):
x = x - 1
con.notify()
print(x)
con.release()
con = threading.Condition()
x = 0
p = Producer('Producer')
c = Consumer('Consumer')
p.start()
p.join()
c.start()
c.join()
|
运行后输出结果如下