039條件變數同步(Condition),039條件變數

來源:互聯網
上載者:User

039條件變數同步(Condition),039條件變數

也是鎖,這個鎖多加了wait(),notify()喚醒一個進程,notifyall()喚醒全部進程方法,建立的時候預設是Rlock類型的鎖,可以設定為lock類型的,預設就ok

 1 from random import randint 2 import threading 3 import time 4  5 class Producer(threading.Thread): 6     def run(self): 7         global L 8         while True: 9             val = randint(0,100)10             print('生產者',self.name,':append',str(val),L)11             if lock_con.acquire():12                 L.append(val)13                 lock_con.notify()14                 lock_con.release()15             time.sleep(3)16 17 class Consumer(threading.Thread):18     def run(self):19         global  L20         while True:21             lock_con.acquire()22             if len(L) == 0:23                 lock_con.wait()24             print('消費者',self.name,'delete',str(L[0]),L)25             del L[0]26             lock_con.release()27         time.sleep(0.5)28 29 if __name__ == '__main__':30     L = []31     lock_con = threading.Condition()32     threads = []33     for i in range(5):34         threads.append(Producer())35         threads.append(Consumer())36     for t in threads:37         t.start()38     for t in threads:39         t.join()
使用例子

 

###########java改編:單生產單消費

 1 import time 2 import threading 3  4 class Res: 5     def __init__(self): 6         self.flag = False 7         self.count = 0 8         self.product = '' 9 10     def set(self,name):11         lock_con.acquire()12         if self.flag:13             lock_con.wait()14         time.sleep(0.00001)15         self.count += 116         self.product = ''.join([name,'**',str(self.count)])17         self.message = ''.join([self.product,'__生產者__',str(threading.current_thread())])18         print(self.message)19         self.flag = True20         lock_con.notify()21         lock_con.release()22 23     def get_product(self):24         lock_con.acquire()25         time.sleep(0.00001)26         if not self.flag:27             lock_con.wait()28         self.message = ''.join([self.product,'__消費者__',str(threading.current_thread())])29         print(self.message)30         self.flag = False31         lock_con.notify()32         lock_con.release()33 34 class Producer(threading.Thread):35     def __init__(self,r):36         threading.Thread.__init__(self)37         self.r = r38 39     def run(self):40         for i in range(100):41             self.r.set('大白兔奶糖')42 43 class Consumer(threading.Thread):44     def __init__(self,r):45         threading.Thread.__init__(self)46         self.r = r47 48     def run(self):49         for i in range(100):50             self.r.get_product()51 52 if __name__ == '__main__':53     lock_con = threading.Condition()54     r = Res()55     c = Consumer(r)56     p = Producer(r)57     c.start()58     p.start()
單生產單消費

 

############多生產多消費

 1 import time 2 import threading 3  4 class Res: 5     def __init__(self): 6         self.flag = False 7         self.count = 0 8         self.product = '' 9 10     def set(self,name):11         lock_con.acquire()12         while self.flag:13             lock_con.wait()14         time.sleep(0.00001)15         self.count += 116         self.product = ''.join([name,'**',str(self.count)])17         self.message = ''.join([self.product,'__生產者__',str(threading.current_thread())])18         print(self.message)19         self.flag = True20         lock_con.notifyAll()21         lock_con.release()22 23     def get_product(self):24         lock_con.acquire()25         time.sleep(0.00001)26         while not self.flag:27             lock_con.wait()28         self.message = ''.join([self.product,'__消費者__',str(threading.current_thread())])29         print(self.message)30         self.flag = False31         lock_con.notifyAll()32         lock_con.release()33 34 class Producer(threading.Thread):35     def __init__(self,r):36         threading.Thread.__init__(self)37         self.r = r38 39     def run(self):40         for i in range(100):41             self.r.set('大白兔奶糖')42 43 class Consumer(threading.Thread):44     def __init__(self,r):45         threading.Thread.__init__(self)46         self.r = r47 48     def run(self):49         for i in range(100):50             self.r.get_product()51 52 if __name__ == '__main__':53     lock_con = threading.Condition()54     r = Res()55     l = []56     for i in range(5):57         l.append(Consumer(r))58     for i in range(5):59         l.append(Producer(r))60     for a in l:61         a.start()
多生產多消費

 

個人覺得例子理解是最好的,所以我學的東西一般使用例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.