gpt4 book ai didi

java - 我可以将 PriorityBlockingQueue 与多个线程一起使用吗?

转载 作者:搜寻专家 更新时间:2023-10-31 08:13:47 27 4
gpt4 key购买 nike

PriorityBlockingQueue 有多少把锁?takeput 操作是否同步?我找不到有关此类队列的太多信息。我使用的是单线程 PriorityQueue

最佳答案

How many locks PriorityBlockingQueue have?

那是无关紧要的实现细节。除非您想了解它是如何实现的,否则我只能建议您查看源代码。

take and put operations are synchronized?

严格来说它们可能不是同步的,但该类是线程安全的,因此您可以在多个线程中同时获取和放入。

注意:PriorityBlockingQueue 的 javadoc 在这一点上不是很明确,但如果你看一下 at the javadoc of the java.util.concurrent package ,你会看到:

Five implementations in java.util.concurrent support the extended BlockingQueue interface, that defines blocking versions of put and take: LinkedBlockingQueue, ArrayBlockingQueue, SynchronousQueue, PriorityBlockingQueue, and DelayQueue.

BlockingQueue clearly states :

BlockingQueue implementations are thread-safe. All queuing methods achieve their effects atomically using internal locks or other forms of concurrency control. However, the bulk Collection operations addAll, containsAll, retainAll and removeAll are not necessarily performed atomically unless specified otherwise in an implementation.

关于java - 我可以将 PriorityBlockingQueue 与多个线程一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398212/

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