Objective
About eating hot pot scenes, small partners are not unfamiliar, eat hot pot when a classmate to the pot under the fish pill, b students at the same time to eat fish pills, it may lead to eat raw fish pills.
In order to avoid this situation, in the process of the next fish pill, the first lock operation, so that the small pot-eating partners to stop for a while, and so on before the fish pill cooked and then open to eat, then python how to simulate this scene?
Not locked
1. If multiple threads are working on a data at the same time, unexpected results can occur. such as the following scenario: When the small partner A in the hot pot to add fish balls, small partner B at the same time eat fish balls, which is likely to lead to the freshly-cooked fish balls are clamped out (not ripe), or have not been pot, to the fish pill (clip).
#Coding=utf-8ImportThreadingImport TimedefChihuoguo (People, do):Print("%s Small Pot-eating partner:%s"%(Time.ctime (), people)) Time.sleep (1) forIinchRange (3): Time.sleep (1) Print("%s%s is being%s fish Ball"%(Time.ctime (), people, do))Print("%s Small Pot-eating partner:%s"%(Time.ctime (), people))classMyThread (Threading. Thread):#inherits the parent class threading. Thread def __init__(self, people, name, do):" "rewrite threading. Thread initialization Content" "Threading. Thread.__init__(self) self.threadname=name Self.people=people self.do= DodefRun (self):#write the code you want to execute into the run function. The thread runs the run function directly after it is created " "overriding the Run method" " Print("Start Thread:"+self.threadname) Chihuoguo (self.people, self.do)#Perform Tasks Print("QQ Exchange Group: 226296743") Print("End Thread:"+self.name)Print("Yoyo Please small partners to start eating hotpot:!!! ")#set up thread groupsThreads = []#Create a new threadThread1 = MyThread ("xiaoming","Thread-1","Add") Thread2= MyThread ("Xiaowang","Thread-2","eaten")#add to Thread groupthreads.append (thread1) threads.append (thread2)#Open Thread forThreadinchThreads:thread.start ()#block main thread, wait for child thread to end forThreadinchThreads:thread.join () time.sleep (0.1)Print("exit main thread: Eat hotpot end, checkout leave")
Operation Result:
Thread synchronization (Locking lock)
1. In order to avoid this situation, the concept of lock is introduced and the lock has two states: locked and unlocked
2. Whenever a thread a wants to access the shared data, it must first obtain a lock, and if there is already another thread B gets locked, then let thread a pause, that is, synchronous blocking, wait until thread B is finished, release the lock, and then let thread a continue.
3. Use Threading. Lock () Two methods in this class
- Acquire () lock
- Release () Free lock
#Coding=utf-8ImportThreadingImport TimedefChihuoguo (People, do):Print("%s Small Pot-eating partner:%s"%(Time.ctime (), people)) Time.sleep (1) forIinchRange (3): Time.sleep (1) Print("%s%s is being%s fish Ball"%(Time.ctime (), people, do))Print("%s Small Pot-eating partner:%s"%(Time.ctime (), people))classMyThread (Threading. Thread):#inherits the parent class threading. ThreadLock= Threading. Lock ()#Thread Lock def __init__(self, people, name, do):" "rewrite threading. Thread initialization Content" "Threading. Thread.__init__(self) self.threadname=name Self.people=people self.do= DodefRun (self):#write the code you want to execute into the run function. The thread runs the run function directly after it is created " "overriding the Run method" " Print("Start Thread:"+self.threadname)#lock a thread before performing a taskSelf.lock.acquire () Chihuoguo (Self.people, self.do)#Perform Tasks #when you're done, release the lockself.lock.release ()Print("QQ Exchange Group: 226296743") Print("End Thread:"+self.name)Print("Yoyo Please small partners to start eating hotpot:!!! ")#set up thread groupsThreads = []#Create a new threadThread1 = MyThread ("xiaoming","Thread-1","Add") Thread2= MyThread ("Xiaowang","Thread-2","eaten")#add to Thread groupthreads.append (thread1) threads.append (thread2)#Open Thread forThreadinchThreads:thread.start ()#block main thread, wait for child thread to end forThreadinchThreads:thread.join () time.sleep (0.1)Print("exit main thread: Eat hotpot end, checkout leave")
Operation Result:
Python note 10-Multi thread synchronization (lock Lock)