看哪個也不如看這個(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
- iOS-Charts看這個就夠了iOS
- cad看圖軟體哪個好用(cad看圖軟體推薦)
- 速看!這10個開源安全工具你知道幾個?
- 怎麼看訪問連結是走了哪個IP
- android線上看原始碼也可以這麼方便Android原始碼
- 真的,刷抖音,不如刷這個專案
- xgboost 特徵重要性選擇 / 看所有特徵哪個重要特徵
- 有關webscraper的問題,看這個就夠了Web
- 那麼多一鍵分發工具哪個最好用?其實看這幾點就夠了
- 不管你是笑是哭看哪裡,這個神經網路都能讓你面無表情神經網路
- 換個角度看原型鏈原型
- HTTP 【值得你看個究竟】HTTP
- 從五個SQL看with as使用SQL
- Mac時鐘軟體哪款好?看這裡吧Mac
- mac上哪款文字處理工具好用?看這裡Mac
- 配音軟體哪個好用?配音小白值得一看的文章
- 一個強大圖片的選擇、裁剪工具—看這一個就夠用了
- [提問交流]看來官方已經拋棄這個框架了框架
- 牛逼至極!用這個神器看程式碼太舒服了
- HTML5開發前景如何?從這四個方面來看HTML
- 這兒有一個資料連線,大家看這兒有哪些問題
- 圖片降噪軟體哪個好?不如試試Topaz DeNoise AIAI
- 智慧電視和電視盒子哪個好?看業內人士怎麼說
- 看某明星偷稅不如看老司機談Kafka的Broker和叢集是什麼回事Kafka
- 別再翻了,面試二叉樹看這 11 個就夠了~面試二叉樹
- 【網路安全】8個網路安全名詞解釋看這裡!
- 股市盛宴,看這個Excel外掛如何玩轉行情資料分析Excel
- 短視訊資料怎麼看?這4個工具超實用
- 【雲端計算】雲端計算四個必學知識看這裡!
- 程式碼是寫給人看的,不是寫個機器看的
- 為什麼說,你純看程式碼而沒有碰到這個場景你就算看懂了也沒法理解?
- vue3 到底哪裡好?看這一篇就夠了Vue
- Flutter適配深色模式(DarkMode):看!你要的黑是這個黑嗎?Flutter模式
- 第14講 | HTTP協議:看個新聞原來這麼麻煩HTTP協議
- MVP+Dagger2+Retrofit2.0+Rxjava看這一個例子就夠了MVPRxJava
- 從一個例子看Go的逃逸分析Go
- 一個理科直男如何看《魷魚遊戲》遊戲