import time
import threading
s1 = threading.Semaphore(5)
def foo(i):
s1.acquire()
print("Enter",threading.current_thread().name,';',i,'sec')
time.sleep(i)
print("Quit", threading.current_thread().name,';',i,'sec')
s1.release()
for i in range(4):
t1 = threading.Thread(target=foo, args=(i,))
t1.start()
t2 = threading.Thread(target=foo, args=(8,))
t2.start()
for i in range(14,18):
t3 = threading.Thread(target=foo, args=(i,))
t3.start()
t4 = threading.Thread(target=foo, args=(23,))
t4.start()
Python线程同步Semaphore实例
最新推荐文章于 2024-07-24 23:49:50 发布