-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_condition.py
More file actions
53 lines (42 loc) · 904 Bytes
/
test_condition.py
File metadata and controls
53 lines (42 loc) · 904 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 28 17:15:29 2018
@author: 18665
"""
import threading
import time
con = threading.Condition()
class Producer(threading.Thread):
# 生产者函数
def run(self):
time.sleep(1)
with con:
print("P get lock")
time.sleep(5)
print("P release")
time.sleep(5)
print("P return")
class Consumer(threading.Thread):
# 消费者函数
def run(self):
with con:
print("C get lock")
time.sleep(5)
print("C relases lock")
print("C reutrn")
# count = 500
def test():
for i in range(2):
p = Producer()
p.start()
for i in range(5):
c = Consumer()
c.start()
def test2():
p = Producer()
p.start()
c = Consumer()
c.start()
if __name__ == '__main__':
test2()
time.sleep(100)