import threading
import time
mutex = threading.Lock()
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global mutex
while 1:
time.sleep(0.5);
if mutex.acquire():
time.sleep(1);
print ('1-----'+time.strftime('%Y-%m-%d-%H-%M-%S'))
mutex.release()
if __name__ == "__main__":
#子线程
my_thread = MyThread()
my_thread.start()
#主线程
while 1:
time.sleep(0.5);
if mutex.acquire():
time.sleep(10)
print ('2----'+time.strftime('%Y-%m-%d-%H-%M-%S'))
mutex.release()
python 线程同步
最新推荐文章于 2024-09-04 10:35:51 发布