foreword
My friends are not unfamiliar with the scene of eating hot pot. When eating hot pot, classmate A puts fish balls in the pot, and classmate B eats fish balls at the same time, which may lead to eating raw fish balls.
In order to avoid this situation, in the process of placing fish balls, first lock the operation, let the friends who eat hot pot stop for a while, and wait for the fish balls to be cooked before eating. So how does python simulate this scenario?
not locked
1. If multiple threads operate on a certain data at the same time, there will be unpredictable results. For example, the following scenario: when friend a is adding fish balls to the hot pot, friend b eats the fish balls at the same time, which is likely to cause the fish balls that have just been placed in the pot to be clipped out (not cooked), or still Before the pot is in, go to the fish balls (can't be caught).
# coding=utf-8 import threading import time def chiHuoGuo(people, do): print ( " %s friends who eat hot pot: %s " % (time.ctime(),people)) time.sleep(1) for i in range(3): time.sleep( 1 ) print ( " %s %s is making fish balls at %s " % (time.ctime(), people, do)) print ( " %s friend who eats hot pot: %s " % (time. ctime(),people)) class myThread (threading.Thread): #Inherit the parent class threading.Thread def __init__ (self, people, name, do): ''' Rewrite the initialization content of threading.Thread ''' threading.Thread .__ init__ (self) self.threadName = name self.people = people self.do = do def run(self): #Write the code to be executed into the run function. After the thread is created, it will run the run function directly. ''' Rewrite the run method ''' print ( " Start thread: " + self.threadName) chiHuoGuo(self.people, self.do) #Execute task print ( " qq communication group: 226296743 " ) print ( " End thread: " + self.name) print ( " yoyo invites friends to eat hot pot:!!! " ) #Set thread group threads = [] #Create a new thread thread1 = myThread( " xiaoming " , " Thread-1 " , " Add " ) thread2 = myThread("xiaowang", "Thread-2", "吃掉") #Add to thread group threads.append(thread1) threads.append(thread2) #Open thread for thread in threads: thread.start() #Block the main thread and wait for the child thread to end for thread in threads: thread.join() time.sleep( 0.1 ) print ( " Exit the main thread: After eating hot pot, check out and leave " )
operation result:
Thread synchronization (lock)
1. In order to avoid the above situation, the concept of lock is introduced. There are two states of lock: locked and unlocked
2. Whenever a thread a wants to access shared data, it must first obtain a lock; if another thread b has already obtained the lock, then let thread a suspend, that is, synchronous blocking; wait until thread b is finished accessing, after releasing the lock , and let thread a continue.
3. Use the two methods in the threading.Lock() class
- acquire() locks
- release() releases the lock
# coding=utf-8 import threading import time def chiHuoGuo(people, do): print ( " %s friends who eat hot pot: %s " % (time.ctime(),people)) time.sleep(1) for i in range(3): time.sleep( 1 ) print ( " %s %s is making fish balls at %s " % (time.ctime(), people, do)) print ( " %s friend who eats hot pot: %s " % (time. ctime(),people)) class myThread (threading.Thread): #Inherit the parent class threading.Thread lock = 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 = do def run(self): #Write the code to be executed into the run function. After the thread is created, it will run the run function directly. ''' Rewrite the run method ''' print ( " Start thread: " + self.threadName) #Lock the thread before executing the task self.lock.acquire() chiHuoGuo(self.people, self.do) #Execute task #After execution, release the lock self.lock.release() print ( " qq exchange group: 226296743 " ) print ( " End thread: " + self.name) print ( " yoyo invites friends to eat hot pot:!!! " ) #Set thread group threads = [] #Create a new thread thread1 = myThread( " xiaoming " , " Thread-1 " , " Add " ) thread2 = myThread("xiaowang", "Thread-2", "吃掉") #Add to thread group threads.append(thread1) threads.append(thread2) #Open thread for thread in threads: thread.start() #Block the main thread and wait for the child thread to end for thread in threads: thread.join() time.sleep( 0.1 ) print ( " Exit the main thread: After eating hot pot, check out and leave " )
operation result: