foreword
When friend a is adding fish balls to the hot pot, this is producer behavior; another friend b is eating fish balls, which is consumer behavior. When the fish balls in the hot pot reach a certain amount and fill up, they can only be eaten. This is a conditional judgment.
This is the Condition (condition variable) to be talked about in this article.
Condition
Condition (condition variable) is usually associated with a lock. When you need to share a lock among multiple Contidions, you can pass a Lock/RLock instance to the constructor, otherwise it will generate an RLock instance by itself.
It can be considered that, in addition to the lock pool with Lock, Condition also contains a waiting pool. The threads in the pool are in the waiting blocking state in the state diagram until another thread calls notify()/notifyAll() notification; after being notified, the thread Enter the lock pool and wait for a lock.
Condition():
- acquire(): thread lock
- release(): release the lock
- wait(timeout): The thread is suspended until it receives a notify notification or timeout (optional, floating point number, in seconds) will be woken up to continue running. wait() must be called only when Lock has been acquired, otherwise a RuntimeError will be triggered.
- notify(n=1): Notify other threads, those suspended threads will start running after receiving this notification. The default is to notify a thread that is waiting for the condition, and at most n waiting threads will be awakened. notify() must be called after Lock has been obtained, otherwise a RuntimeError will be triggered. notify() will not actively release the Lock.
- notifyAll(): If there are many threads in the wait state, the function of notifyAll is to notify all threads
Producer and Consumer
Realization scenario: when classmate a adds fish balls to the Wang hot pot (up to 5, notify b to eat them after filling), inform class b to eat the fish balls (when it reaches 0, inform class a to continue adding)
# coding=utf-8 import threading import time con = threading.Condition() num = 0 #Producer class Producer (threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): #Lock thread global num con.acquire () while True: print " Start adding!!! " num += 1 print " Number of fish balls in the hot pot: %s " % str(num) time.sleep( 1 ) if num >= 5 : print "The number of fish balls in the hot pot has reached 5 and cannot be added! " #Wake up the waiting thread con.notify() #Wake up the friends to eat #Wait for notification con.wait() #release lock con.release() #Consumer class Consumers (threading.Thread): def __init__ (self): threading.Thread.__init__(self) def run(self): con.acquire () global num while True: print " Start eating!!! " num -= 1 print " Number of fish balls left in the hot pot: %s " % str(num) time.sleep( 2 ) if num <= 0: print "The bottom of the pot is out of stock, hurry up and add fish balls! " con.notify() #Wake up other threads #Waiting for notification con.wait() con.release() p = Producer() c = Consumers() p.start() c.start()
Running result:
start adding! ! !
Number of fish balls in the hot pot: 1
Start adding! ! !
Number of fish balls in the hot pot: 2
Start adding! ! !
Number of fish balls in the hot pot: 3
Start adding! ! !
Number of fish balls in the hot pot: 4
and start adding! ! !
The number of fish balls in the hot pot: 5 The number of
fish balls in the hot pot has reached 5 and cannot be added!
Start eating! ! !
Number of fish balls left in the hot pot: 4
Let’s eat! ! !
Number of fish balls left in the hot pot: 3
Let’s start eating! ! !
Number of fish balls left in the hot pot: 2
Let’s start eating! ! !
The number of fish balls left in the hot pot: 1
Let’s start eating! ! !
The number of fish balls left in the hot pot: 0
The bottom of the pot is out of stock, hurry up and add fish balls!
Start adding! ! !
Number of fish balls in the hot pot: 1
Start adding! ! !
Number of fish balls in the hot pot: 2
Start adding! ! !
Number of fish balls in the hot pot: 3
Start adding! ! !
Number of fish balls in the hot pot: 4
and start adding! ! !
Number of fish balls in hot pot: 5