信号量

复制代码
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()
复制代码

 

posted @   阜阳小全  阅读(93)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示