039 Condition variable synchronization (Condition), 039 Condition variable
The lock adds wait (), notify () to wake up a process, and notifyall () to wake up all process methods. The Rlock type lock is used by default during creation, can be set to lock type, the default is 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 ('produser', self. name, ': append', str (val), L) 11 if lock_con.acquire (): 12 L. append (val) 13 lock_con.y y () 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 ('consumer ', 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 ()
Example
########## Java Adaptation: single production and single consumption
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, '_ producer _', 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, '_ consumer _', str (threading. current_thread ()]) 29 print (self. message) 30 self. flag = False31 lock_con.y y () 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 ('White Rabbit toffge') 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 ()
Single production and single consumption
########### Multi-production and multi-Consumption
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, '_ producer _', str (threading. current_thread ()]) 18 print (self. message) 19 self. flag = True20 lock_con.policyall () 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, '_ consumer _', str (threading. current_thread ()]) 29 print (self. message) 30 self. flag = False31 lock_con.policyall () 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 ('White Rabbit toffge') 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. start ()
Multi-production and multi-Consumption
I personally think that examples are the best to understand.