• 并发编程四(2) 线程同步 Semaphore加锁


    Semaphore-加锁

    from threading import Thread, Semaphore
    import threading
    import time
    
    def worker(s,i):
        s.acquire()
        print(threading.current_thread().name + " acquire")
        time.sleep(i*2)
        print(threading.current_thread().name + " release")
        s.release()
    
    if __name__ == "__main__":
        # 设置限制最多3个线程同时访问共享资源
        s = Semaphore(3)
        for i in range(5):
            t = Thread(target = worker, args = (s, i * 2))
            t.start()
  • 相关阅读:
    java8
    java7
    java6
    java5
    java复习4
    学习笔记
    Reflection笔记
    通过Reflection来获得方法和信息
    學習反射2
    學習反射1
  • 原文地址:https://www.cnblogs.com/kknote/p/16103452.html
Copyright © 2020-2023  润新知