看哪個也不如看這個(Condition Objects)
15.3.3 Condition Objects
A condition variable is always associated with some kind of lock; this can be passed in or one will be created by default. (Passing one in is useful when several condition variables must share the same lock.)
A condition variable has acquire() and release() methods that call the corresponding methods of the associated lock. It also has a wait() method, and notify() and notifyAll() methods. These three must only be called when the calling thread has acquired the lock.
The wait() method releases the lock, and then blocks until it is awakened by a notify() or notifyAll() call for the same condition variable in another thread. Once awakened, it re-acquires the lock and returns. It is also possible to specify a timeout.
The notify() method wakes up one of the threads waiting for the condition variable, if any are waiting. The notifyAll() method wakes up all threads waiting for the condition variable.
Note: the notify() and notifyAll() methods don't release the lock; this means that the thread or threads awakened will not return from their wait() call immediately, but only when the thread that called notify() or notifyAll() finally relinquishes ownership of the lock.
Tip: the typical programming style using condition variables uses the lock to synchronize access to some shared state; threads that are interested in a particular change of state call wait() repeatedly until they see the desired state, while threads that modify the state call notify() or notifyAll() when they change the state in such a way that it could possibly be a desired state for one of the waiters. For example, the following code is a generic producer-consumer situation with unlimited buffer capacity:
# Consume one item cv.acquire() while not an_item_is_available(): cv.wait() get_an_available_item() cv.release() # Produce one item cv.acquire() make_an_item_available() cv.notify() cv.release()
To choose between notify() and notifyAll(), consider whether one state change can be interesting for only one or several waiting threads. E.g. in a typical producer-consumer situation, adding one item to the buffer only needs to wake up one consumer thread.
class Condition( | [lock]) |
None
, it must be a Lock or RLock object, and it is used as the underlying lock. Otherwise, a new RLock object is created and used as the underlying lock.
acquire( | *args) |
release( | ) |
wait( | [timeout]) |
This method releases the underlying lock, and then blocks until it is awakened by a notify() or notifyAll() call for the same condition variable in another thread, or until the optional timeout occurs. Once awakened or timed out, it re-acquires the lock and returns.
When the timeout argument is present and not None
, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).
When the underlying lock is an RLock, it is not released using its release() method, since this may not actually unlock the lock when it was acquired multiple times recursively. Instead, an internal interface of the RLock class is used, which really unlocks it even when it has been recursively acquired several times. Another internal interface is then used to restore the recursion level when the lock is reacquired.
notify( | ) |
This method wakes up one of the threads waiting for the condition variable, if any are waiting; it is a no-op if no threads are waiting.
The current implementation wakes up exactly one thread, if any are waiting. However, it's not safe to rely on this behavior. A future, optimized implementation may occasionally wake up more than one thread.
Note: the awakened thread does not actually return from its wait() call until it can reacquire the lock. Since notify() does not release the lock, its caller should.
notifyAll( | ) |
相關文章
- 生活自媒體素材從哪找?找素材看這個
- 看過無數Java GC文章,這5個問題你也未必知道!JavaGC
- cad看圖軟體哪個好用(cad看圖軟體推薦)
- 我與這個世界格格不如
- 速看!這10個開源安全工具你知道幾個?
- [養兒防老]這個觀念你怎麼看?
- android線上看原始碼也可以這麼方便Android原始碼
- 需要待看或已看文章,做個記錄
- xgboost 特徵重要性選擇 / 看所有特徵哪個重要特徵
- 有關webscraper的問題,看這個就夠了Web
- 看過這個,你可能更瞭解指標3指標
- 快來看!這兒有個人比你還不要臉!
- 換個角度看原型鏈原型
- HTTP 【值得你看個究竟】HTTP
- 真的,刷抖音,不如刷這個專案
- 《港詭實錄》:搞恐怖和看大腿究竟哪個更重要?
- c#表示式樹入門,看這個就夠了C#
- 不管你是笑是哭看哪裡,這個神經網路都能讓你面無表情神經網路
- 那麼多一鍵分發工具哪個最好用?其實看這幾點就夠了
- 配音軟體哪個好用?配音小白值得一看的文章
- [提問交流]看來官方已經拋棄這個框架了框架
- 看過這個,你可能更瞭解指標一點(2)指標
- HTML5開發前景如何?從這四個方面來看HTML
- 牛逼至極!用這個神器看程式碼太舒服了
- mac上哪款文字處理工具好用?看這裡Mac
- Mac時鐘軟體哪款好?看這裡吧Mac
- 看過的文件地址——個人留存
- 想知道HBC到底怎麼回事兒?來看這個視訊吧!
- 股市盛宴,看這個Excel外掛如何玩轉行情資料分析Excel
- 短視訊資料怎麼看?這4個工具超實用
- 【網路安全】8個網路安全名詞解釋看這裡!
- 看某明星偷稅不如看老司機談Kafka的Broker和叢集是什麼回事Kafka
- 圖片降噪軟體哪個好?不如試試Topaz DeNoise AIAI
- Flutter適配深色模式(DarkMode):看!你要的黑是這個黑嗎?Flutter模式
- 別再翻了,面試二叉樹看這 11 個就夠了~面試二叉樹
- 第14講 | HTTP協議:看個新聞原來這麼麻煩HTTP協議
- 看基類被那幾個類了
- 拿個錘子看誰都是釘子
- 從一個例子看Go的逃逸分析Go