import threading,time
def run(n):
semaphore.acquire()
time.sleep(1)
print(‘run the thread:%s\n‘ % n)
semaphore.release()
if __name__ ==‘__main__‘:
semaphore = threading.BoundedSemaphore(5)#信号量
for i in range(20):
t = threading.Thread(target = run,args=(i,))
t.start()
while threading.active_count() != 1:
pass
else:
print(‘---all threads done‘)
原文:http://www.cnblogs.com/zhangjixiang/p/7770877.html