import threading ,time
a = 5
alock = threading.Lock()
b = 5
block = threading.Lock()
def thread1calc():
print "thread1 acquiring lock a"
alock.acquire()
time.sleep(5)
print "thread1 acquiring lock b"
block.acquire()
a+=5
b+=5
print "thread1 releasing both locks"
block.release()
alock.release()
def thread2calc():
print "thread2 acquiring lock b"
block.acquire()
time.sleep(5)
print "thread2 acquiring lock a"
alock.acquire()
time.sleep(5)
a+=10
b+=10
print "thread2 releasing both locks"
block.release()
alock.release()
t = threading.Thread(target = thread1calc)
t.setDaemon(1)
t.start()
t = threading.Thread(target = thread2calc)
t.setDaemon(2)
t.start()
while 1:
time.sleep(300)
输出:
thread1 acquiring lock a
thread2 acquiring lock b
thread1 acquiring lock b
thread2 acquiring lock a
希望本文所述对大家的Python程序设计有所帮助。
本文原创发布php中文网,转载请注明出处,感谢您的尊重!
相关文章
相关视频