Objective
When little buddy A is adding fish balls to the pot, this is the producer's behavior; another small partner B eats fish balls is consumer behavior. When the pot inside the fish to reach a certain number of fill after the B to eat, this is a condition to judge.
This is the condition (condition variable) to be said in this article
Condition
A Condition (condition variable) is usually associated with a lock. When you need to share a lock in multiple contidion, you can pass an Lock/rlock instance to the constructor method, or it will generate itself a rlock instance.
It can be assumed that, in addition to the lock pool with lock, the condition also contains a wait pool in which the thread in the pool waits for blocking state until another thread calls notify ()/notifyall () notification, and the thread enters the lock pool to wait for the lock after being notified.
Condition ():
- Acquire (): Thread lock
- Release (): Releasing the lock
- Wait (timeout): The thread hangs until it receives a notify notification or timeout (optional, floating-point number, in seconds s) before it wakes up and continues to run. Wait () must be called before the lock is obtained, or runtimeerror will be triggered.
- Notify (N=1): Notifies other threads that the suspended thread will start running after receiving this notification, by default notifying a thread that is waiting for the condition, up to n waiting threads. Notify () must be called before the lock is obtained, or runtimeerror will be triggered. Notify () does not voluntarily release lock.
- Notifyall (): If the wait state thread is more, the Notifyall is to notify all threads
Producers and consumers
Implementation of the scene: When a classmate Wang inside add fish pills filled (up to 5, after filling the notice B to eat), inform B students to eat fish pills (eat to 0 when notify a classmate continue to add)
#Coding=utf-8ImportThreadingImportTimecon=Threading. Condition () Num=0#producersclassProducer (Threading. Thread):def __init__(self): threading. Thread.__init__(self)defRun (self):#Lock Thread Globalnum con.acquire () whileTrue:Print "start adding!!! "Num+ = 1Print "number of fish balls inside Hotpot:%s"%str (num) time.sleep (1) ifNum >= 5: Print "The number of fish balls inside the pot has reached 5 and cannot be added! " #wake-on-waiting threadCon.notify ()#wake up the Little buddy and eat. #Wait for notificationcon.wait ()#Release Lockcon.release ()#ConsumerclassConsumers (threading. Thread):def __init__(self): threading. Thread.__init__(self)defRun (self): Con.acquire ()GlobalNum whileTrue:Print "start eating!!! "Num-= 1Print "number of remaining fish balls inside Hotpot:%s"%str (num) time.sleep (2) ifNum <=0:Print "The bottom of the pot is not in stock, quickly add fish pills! "con.notify ()#Waking Other Threads #Wait for notificationcon.wait () con.release () p=Producer () C=consumers () P.start () C.start ( )
Operation Result:
Start adding!!!
Pot inside Fish Ball Number: 1
Start adding!!!
Pot inside Fish Ball Number: 2
Start adding!!!
Pot inside Fish Ball Number: 3
Start adding!!!
Pot inside Fish Ball Number: 4
Start adding!!!
Pot inside Fish Ball Number: 5
The number of fish balls inside the pot has reached 5 and cannot be added!
Start eating!!!
Pot inside the remaining number of fish pills: 4
Start eating!!!
Pot inside the remaining number of fish pills: 3
Start eating!!!
Pot inside the remaining number of fish pills: 2
Start eating!!!
Pot inside the remaining number of fish pills: 1
Start eating!!!
Pot inside the remaining number of fish pills: 0
The bottom of the pot is not in stock, quickly add fish pills!
Start adding!!!
Pot inside Fish Ball Number: 1
Start adding!!!
Pot inside Fish Ball Number: 2
Start adding!!!
Pot inside Fish Ball Number: 3
Start adding!!!
Pot inside Fish Ball Number: 4
Start adding!!!
Pot inside Fish Ball Number: 5
Python note 11-Multi-thread condition (condition variable)