-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.py
More file actions
60 lines (44 loc) · 1.17 KB
/
07.py
File metadata and controls
60 lines (44 loc) · 1.17 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
import threading
import time
lock_1 = threading.Lock()
lock_2 = threading.Lock()
def fun1():
print("func1 starting......")
lock_1.acquire(timeout = 4)
print("func1 acquir1 lock_1")
time.sleep(2)
print("func_1 等待lock_2")
rst=lock_2.acquire()
if rst:
print("func_1 acquire lock_2......")
lock_2.release()
print("func_1 relesse lock_2")
else:
print("fun1 not acquire lock_2")
lock_1.release()
print("func1 release lock_1")
print("func1 done......")
def func_2():
print("func2 starting......")
lock_2.acquire()
print("func2 acquir1 lock_2")
time.sleep(4)
print("func_2 等待lock_1")
rst = lock_1.acquire(timeout=4)
if rst:
print("func_2 acquire lock_1......")
lock_1.release()
print("func_2 relesse lock_1")
else:
print("func_2 not acquire lock_1")
lock_2.release()
print("func1 release lock_2")
print("func1 done......")
if __name__ == '__main__':
print("主程序启动")
t1 = threading.Thread(target=fun1, args=())
t2 = threading.Thread(target=func_2, args=())
t1.start()
t2.start()
t1.join()
t2.join()