forked from andaok/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadRlock.py
More file actions
52 lines (33 loc) · 1.12 KB
/
ThreadRlock.py
File metadata and controls
52 lines (33 loc) · 1.12 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
# -*- encoding:utf-8 -*-
'''
Created on Sep 18, 2012
@author: root
write by wye in cloudiya
'''
import threading
import time
rlock = threading.RLock()
def func():
#第一次请求锁定
print "%s acquire lock first..." % threading.currentThread().getName()
if rlock.acquire():
print "%s get lock first..." % threading.currentThread().getName()
time.sleep(4)
#第二次请求锁定
print "%s acquire lock second..." % threading.currentThread().getName()
if rlock.acquire():
print "%s get lock second..." % threading.currentThread().getName()
time.sleep(4)
#第一次释放锁
print "%s release lock first..." % threading.currentThread().getName()
rlock.release()
time.sleep(4)
#第二次释放锁
print "%s release lock second..." % threading.currentThread().getName()
rlock.release()
t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)
t3 = threading.Thread(target=func)
t1.start()
t2.start()
t3.start()