参考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html 

semaphore 信号量:

condition 条件变量:

event 同步条件:条件同步和条件变量同步差不多意思,只是少了锁功能。因为条件同步设计于别访问共享资源的条件环境

 

多线程利器(queue):队列本身有一把锁

q.put(‘xiaoming’, 0)

q.get(0)

q.qsize()  返回队列大小

q.empty()

q.full()

semaphore:

# _author: lily
# _date: 2019/1/29

import threading , time

class MyThread(threading.Thread):
    def run(self):
        if semaphore.acquire():
            print(self.name)
            time.sleep(3)
            semaphore.release()

if __name__ == '__main__':
    semaphore = threading.BoundedSemaphore(5)
    thread_list = []
    for i in range(100):
        thread_list.append(MyThread())
    for i in thread_list:
        i.start()
View Code

相关文章:

  • 2021-08-03
  • 2022-12-23
  • 2021-10-13
  • 2021-11-22
  • 2021-11-17
  • 2021-06-24
  • 2021-06-14
  • 2021-11-18
猜你喜欢
  • 2021-10-10
  • 2022-12-23
  • 2022-02-12
  • 2022-12-23
  • 2021-10-10
  • 2021-11-28
  • 2022-12-23
相关资源
相似解决方案