zoukankan      html  css  js  c++  java
  • lock(1次放1个):

    import threading
    import time
    v = []
    lock = threading.Lock()
    def func(arg):
        v.append(arg)
        time.sleep(0.01)
        m = v[-1]
        print(arg,m)
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    结果:
    01 9
     9
    3 9
    24 9
     9
    7 5 9
    6 99
    
    8 9
    9 9
    线程不安全
    import threading
    import time
    v = []
    lock = threading.Lock()
    def func(arg):
        lock.acquire()
        v.append(arg)
        time.sleep(0.01)
        m = v[-1]
        print(arg,m)
        lock.release()
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    线程安全

      lock只能加一把锁,加多把锁就会产生死锁.

    Rlock(1次放1个):

    import threading
    import time
    v = []
    lock = threading.RLock()
    def func(arg):
        lock.acquire()
        lock.acquire()
        v.append(arg)
        time.sleep(0.01)
        m = v[-1]
        print(arg,m)
        lock.release()
        lock.release()
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    结果:
    0 0
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    RLock

      Rlock可以加多把锁.

    BoundedSemaphore(1次放N个)信号量(N:指定数量个):

    import time
    import threading
    lock = threading.BoundedSemaphore(3)
    def func(arg):
        lock.acquire()
        print(arg)
        time.sleep(1)
        lock.release()
    for i in range(20):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    结果:
    0
    1
    2
    3
    5
    4
    6
    7
    8
    9
    1011
    
    12
    13
    14
    15
    1716
    
    18
    19
    View Code

    Condition(1次放x个)(x:不固定数量个):

    import time
    import threading
    lock = threading.Condition()
    def func(arg):
        print('线程进来了')
        lock.acquire()
        lock.wait() # 加锁
        print(arg)
        time.sleep(1)
        lock.release()
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    while True:
        inp = int(input('>>>'))
        lock.acquire()
        lock.notify(inp)
        lock.release()
    方式一
    import time
    import threading
    lock = threading.Condition()
    def xxxx():
        print('来执行函数了')
        input(">>>")
        ct = threading.current_thread() # 获取当前线程
        ct.getName()
        return True
    
    def func(arg):
        print('线程进来了')
        lock.wait_for(xxxx)
        print(arg)
        time.sleep(1)
    
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    方式二

    Event(1次放所有):

    import time
    import threading
    lock = threading.Event()
    def func(arg):
        print('线程来了')
        lock.wait() # 加锁:红灯
        print(arg)
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    input(">>>>")
    lock.set() # 绿灯
    lock.clear() # 再次变红灯
    for i in range(10):
        t =threading.Thread(target=func,args=(i,))
        t.start()
    input(">>>>")
    lock.set()
    红绿灯
  • 相关阅读:
    nginx重启失败的解决方法
    Linux开机禁用开启防火墙
    Linux查看系统版本信息的几种方法
    Linux修改时区的正确方法
    Linux中awk的gsub函数用法
    Linux 的mktemp 命令详解
    tomcat的简单概要小结
    {面试题8: 旋转数组的最小数字}
    {面试题7: 使用两个队列实现一个栈}
    {面试题7: 用两个栈实现队列}
  • 原文地址:https://www.cnblogs.com/Virous1887/p/9628388.html
Copyright © 2011-2022 走看看