信号量

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 @ 2018-05-04 17:56  阜阳小全  阅读(93)  评论(0编辑  收藏  举报