简体   繁体   English

我应该声明java.util.concurrent.ConcurrentLinkedQueue引用volatile吗?

[英]Should I declare java.util.concurrent.ConcurrentLinkedQueue references volatile?

I am using a java.util.concurrent.ConcurrentLinkedQueue object to pass data between threads. 我正在使用java.util.concurrent.ConcurrentLinkedQueue对象在线程之间传递数据。

Should I declare my references volatile ? 我应该声明我的引用是否volatile

In short, no. 简而言之,没有。

The value that your queue variable contains is a reference to the queue. 队列变量包含的值是对队列的引用。 This value won't change unless you reassign the queue like myQueue = otherQueue; 除非您像myQueue = otherQueue;一样重新分配队列,否则此值不会更改myQueue = otherQueue; If all you are doing afer you create the queue is putting things in and taking things out then it doesn't matter if a thread has a cached value because the value (the reference to the queue) never changes. 如果你正在做的就是你创建队列就是把事情放进去并把事情拿出去,那么线程是否有缓存值并不重要,因为值(对队列的引用)永远不会改变。

It's good practice to make all variables final unless you need it to not be final. 最好将所有变量设为最终,除非你需要它不是最终变量。

No, since you are always using the same queue. 不,因为您总是使用相同的队列。 Volatile means, that the value of a variable in memory will always be the same for every processor. 易失性意味着内存中变量的值对于每个处理器总是相同的。 Note that variables that are stored in a register will not be synchronised, even if you declare the variable as volatile. 请注意,即使将变量声明为volatile,也不会同步存储在寄存器中的变量。

If you can declare it final, then declare it final. 如果您可以将其声明为final,则将其声明为final。 If you cannot, declare it volatile. 如果你不能,宣布它是不稳定的。

Declaring it volatile has no effects on the inner structure of the Queue though. 声明它不稳定对Queue的内部结构没有影响。 It only adds additional synchronization when assigning the a new ConcurrentLinkedQueue() 它只在分配new ConcurrentLinkedQueue()时添加了额外的同步

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Queue.poll 比 Iteration 快? (java.util.concurrent.ConcurrentLinkedQueue) - Why is Queue.poll faster than Iteration? (java.util.concurrent.ConcurrentLinkedQueue) java.util.concurrent.ConcurrentLinkedQueue#head 初始化后如何更新? - How is java.util.concurrent.ConcurrentLinkedQueue#head updated after initialisation? java.util.ConcurrentLinkedQueue - java.util.ConcurrentLinkedQueue 我应该声明字段volatile吗? - Should I declare field volatile? 使用java.util.concurrent.Concurrent *容器时使用volatile有什么用? - What does using volatile when using a java.util.concurrent.Concurrent* containers provide? 在使用java.util.concurrent类时,我应该同步以避免可见性问题吗? - Should I synchronize to avoid visiblity issues when using java.util.concurrent classes? 如何在并发hashmap中声明volatile列表? - How to declare a volatile list within a concurrent hashmap? java.util.concurrent.Future中的方法cancel()是否应该阻塞? - Whether method cancel() in java.util.concurrent.Future should be blocking? java.util.concurrent中 - java.util.concurrent 我可以将java.util.concurrent.locks.Lock更改为java.util.concurrent.locks.ReentrantReadWriteLock; - Can i change java.util.concurrent.locks.Lock into java.util.concurrent.locks.ReentrantReadWriteLock;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM