Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
利用condition对象
存钱时,将新的余额更新,然后使用notify_all()通知所有等待的线程,存钱操作已完成。
取钱操作:
取钱时,如果余额不足,线程会等待,直到有其他线程存钱,并调用notify_all()通知。
一旦余额足够,取钱操作将继续,更新余额。
多线程执行:
使用ThreadPoolExecutor创建5个存钱线程和5个取钱线程。
通过这种方式,可以保证在多个线程并发操作银行账户时,余额的更新是安全的,并且在余额不足时,取钱操作会等待,直到有足够的余额。