gpt4 book ai didi

java - Java 中 BlockingQueue 上的并发 put 调用

转载 作者:行者123 更新时间:2023-11-30 05:00:49 24 4
gpt4 key购买 nike

我知道并发添加到 c++ 中的 STL 队列可能会导致问题,解决此问题的方法是在所有添加/删除调用周围添加互斥锁。

但我现在正在用 Java 编程,并且正在使用 BlockingQueue。文档只说对 BlockingQueue 对象调用 put/take 的线程会被隐式阻塞,直到有空间可以放置/有东西可以分别取出。但是,它没有提及任何有关并发 put/take 调用的内容。我需要用互斥锁来保护它们吗?

最佳答案

不,阻塞队列是线程安全的。来自 the docs :

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control

关于java - Java 中 BlockingQueue 上的并发 put 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com