标签:假设 部分 threading lan release 并且 and 爱情故事 例子
from threading import Thread,Lock
import time
mutexA=Lock()
mutexB=Lock()
class MyThread(Thread):
def run(self):
self.func1()
self.func2()
def func1(self):
mutexA.acquire()
print(‘\033[41m%s 拿到A锁\033[0m‘ %self.name)
mutexB.acquire()
print(‘\033[42m%s 拿到B锁\033[0m‘ %self.name)
mutexB.release()
mutexA.release()
def func2(self):
mutexB.acquire()
print(‘\033[43m%s 拿到B锁\033[0m‘ %self.name)
time.sleep(2)
mutexA.acquire()
print(‘\033[44m%s 拿到A锁\033[0m‘ %self.name)
mutexA.release()
mutexB.release()
if __name__ == ‘__main__‘:
for i in range(5):
t=MyThread()
t.start()
从前有两个线程,它们分别叫线程1和线程2(假设没有其他线程跟它俩抢东西)
标签:假设 部分 threading lan release 并且 and 爱情故事 例子
原文地址:https://www.cnblogs.com/zlx960303/p/12925624.html