多个线程通过互斥锁有序的执行

from threading import Thread,Lock
from time import sleep

class Task1(Thread):
	"""y';]lopppdocstring for Task1"""
	def run(self):

	 	while 1:
	 		if lock1.acquire():
	 			print("---task1----")
	 			sleep(1)
	 			lock2.release()


class Task2(Thread):
	"""docstring for Task2"""
	def run(self):
		while 1:
			if lock2.acquire():
				print("---task2----")
				sleep(1)
				lock3.release()

		
class Task3(Thread):
	"""docstring for Task3"""
	def run(self):
		while 1:
			if lock3.acquire():
				print("---task3----")
				sleep(1)
				lock1.release()
		

#创建3把锁
#使用lock创建的锁,默认没有“上锁”
lock1 = Lock()

lock2 = Lock()
lock2.acquire()

lock3 = Lock()
lock3.acquire()


t1 = Task1()
t2 = Task2()
t3 = Task3()

t1.start()
t2.start()
t3.start()

  

上一篇:开工:创建虚拟机,xshell连接虚拟机,复制虚拟机,docker安装,添加加速器


下一篇:中文汉字翻译为相应的拼音