zoukankan      html  css  js  c++  java
  • python线程之condition

    cond = threading.Condition()

    # 类似lock.acquire()

    cond.acquire()

    # 类似lock.release()

    cond.release()

    # 等待指定触发,同时会释放对锁的获取,直到被notify才重新占有琐。

    cond.wait()

    # 发送指定,触发执行

    cond.notify()

    以下分别为两个线程,三个线程,四个线程。可直接看四个线程的运行过程,更加直观,有时候用文字解释不如直接实验让人理解的快。

    import threading,time
    
    cond = threading.Condition()
    #cond.acquire()
    #cond.release()
    #cond.wait()
    #cond.notify()
    def aa():
        print ('thread_aa get')
        cond.acquire()
        time.sleep(5)
        print ('thread_aa finished first job')
        cond.wait()#释放对锁的控制,好让别的进程acquire到
                   #同时阻塞在此,等待得到锁的那个进程的先cond.notify后(cond.wait或cond.release)
        print ('thread_aa get again')
        time.sleep(5)
        print ('thread_aa finished all work')
        cond.notify()#告诉刚才释放锁的那个进程等通知吧,等我要么wait要么release
        cond.release()#释放锁
    def bb():
        cond.acquire()
        print ('thread_bb get')
        cond.notify()#告诉刚才释放锁的那个进程等通知吧,等我要么wait要么release
        print ('依然是我bb在运行')
        time.sleep(5)
        print ('thread_bb finished first job')
        cond.wait()#释放对锁的控制,而后被阻塞的aa就可以执行了
                #同时阻塞在此,等待得到锁的aa的先cond.notify后(cond.wait或cond.release)
        print ('thread_aa get again')    
        print ('thread_bb finished all works')
        cond.release()
        
    a=threading.Thread(target=aa)
    a.start()
    time.sleep(2)
    b=threading.Thread(target=bb)
    b.start()
    import threading,time
    
    cond = threading.Condition()
    def aa():
        cond.acquire()
        time.sleep(1)
        print ('aa1')
        cond.wait()
        time.sleep(1)
        print ('aa2')
        cond.notify()
        cond.wait()
        print ('aa3')
        cond.notify()
        cond.wait()
        print ('aa4')
        cond.release()
    def bb():
        cond.acquire()
        print ('bb1')
        cond.wait()#没notify而直接wait,就是将对a的控制权抛出,但是自己的控
                               # 制权还在a那里,任意一个 acquire到锁的进城将得到对a的控
                                 # 制权(除非他自己又抛出),而这个acquire到锁的进程的控制
                                # 权在这个进程,如果这个进程在之后退出了,那么他控制的
                                 # 进程的控制权将转交给控制他的进程
                            
        print ('bb2')
        cond.notify()
        cond.release()
    def cc():
        cond.acquire()
        print ('cc1')
        cond.notify()
        cond.wait()
        print ('cc2')
        cond.notify()
        cond.wait()
        print ('cc3')
        cond.notify()
        cond.release()   
    a=threading.Thread(target=aa)
    a.start()
    time.sleep(2)
    b=threading.Thread(target=bb)
    b.start()
    time.sleep(2)
    c=threading.Thread(target=cc)
    c.start()
    
    '''这是三个以上版本的,或许更加直观。运行结果:
    aa1
    bb1
    cc1
    aa2
    bb2
    cc2
    aa3
    cc3
    aa4
    '''
    import threading,time
    
    cond = threading.Condition()
    def aa():
        cond.acquire()
        time.sleep(1)
        print ('aa1')
        cond.wait()
        time.sleep(1)
        print ('aa2')
        cond.wait()
        print ('aa3')
        cond.notify()
        cond.wait()
        print ('aa4')
        cond.release()
    def bb():
        cond.acquire()
        print ('bb1')
        cond.wait()
                            
        print ('bb2')
        cond.notify()
        cond.release()
    def cc():
        cond.acquire()
        print ('cc1')
        cond.notify()
        cond.wait()
        print ('cc2')
        cond.notify()
        cond.wait()
        print ('cc3')
        cond.notify()
        cond.release()
    def dd(): 
        cond.acquire()
        print ('dd1')
        cond.notify()
        cond.release()
    a=threading.Thread(target=aa)
    a.start()
    time.sleep(2)
    b=threading.Thread(target=bb)
    b.start()
    time.sleep(2)
    c=threading.Thread(target=cc)
    c.start()
    time.sleep(2)
    d=threading.Thread(target=dd)
    d.start()
    
    
    
    
    
    
    
    
    '''这是4个版本的,运行结果:
    aa1
    bb1
    cc1
    aa2
    dd1
    bb2
    cc2
    aa3
    cc3
    aa4
    '''
  • 相关阅读:
    奔溃瞬间1
    面试知识点blog汇总
    贪心
    树 和 图
    DFS 和 BFS
    STL
    哈希表
    手写堆
    并查集
    二项式反演学习笔记
  • 原文地址:https://www.cnblogs.com/saolv/p/9330740.html
Copyright © 2011-2022 走看看