#!/usr/bin/env python import threading,time def run(n): # 申请锁 semaphore.acquire() time.sleep(1) print("run thre thread:%s "%n) # 释放锁 semaphore.release() if __name__ == "__main__": # 申请线程池,每次最多开辟五个线程 semaphore = threading.BoundedSemaphore(5) for i in range(22): # 开启一个线程 t = threading.Thread(target=run,args=(i,)) t.start() while threading.active_count() !=1: pass else : print("all threads done")