gpt4 book ai didi

java - LinkedTransferQueue 线程安全吗?

转载 作者:行者123 更新时间:2023-12-02 12:38:14 31 4
gpt4 key购买 nike

并发链接队列的Java文档明确指出它是无界线程安全队列。然而,链接传输队列的 javadoc 仅提到队列的无界性质,而没有提及线程安全

我指的不是转账方式。

生产者调用add方法,消费者调用poll方法。

最佳答案

简而言之,答案是肯定的,类 j.u.c.LinkedTransferQueue 是线程安全的。由于集合类是线程安全的,您可以从任何线程安全地调用它的任何方法,包括 addpoll

javadoc 中的以下文字应被视为证明:

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a LinkedTransferQueue happen-before actions subsequent to the access or removal of that element from the LinkedTransferQueue in another thread.

此外,j.u.c.BlockingQueue 在单线程环境中没有多大意义。我的意思是您可以使用它,但是还有更轻量级的解决方案,例如简单的 j.u.Queue 接口(interface)。 BlockingQueue 的主要应用领域是生产者-消费者应用程序,其中消费者能够阻塞等待下一个元素,该元素可能仅来自另一个线程,因为当前线程被阻塞。由于 j.u.c.TransferQueue 扩展了它,因此它的实现也应该是线程安全的。

关于java - LinkedTransferQueue 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45053705/

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