import time,threading class MyThread(threading.Thread): def run(self): if semaphore.acquire(): print(self.name) time.sleep(1) semaphore.release() if __name__ == '__main__': semaphore = threading.Semaphore(5) #意思是开5个线程 semaphoe也是锁的一种 thrs = [] for i in range(100): thrs.append(MyThread()) for i in thrs: i.start()