AtomicBoolean,设置标志一次,必要吗?静态布尔值可以吗? [英] AtomicBoolean, set flag once, necessary? Might a static boolean be ok?

查看:571
本文介绍了AtomicBoolean,设置标志一次,必要吗?静态布尔值可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个标志,该标志由任何设置它的线程设置一次。所有其他线程将在不同的时间,经常读取此标志重复。

I am setting a flag which is set once by any thread that get to set it. All other threads will at various time, pretty often read this flag repeateadly.

现在我正在使用AtomicBoolean,它工作正常,但我知道如果经常查询它可能比普通布尔慢得多(不确定是否这是真的 )。

Right now I am using an AtomicBoolean, which works fine, but I know that if it is queried quite often it can be considerably slower than plain boolean, ( not sure if this is true ).

将此更改为静态布尔值是否可以线程安全?无论谁做到这一点,都将标志设置为true,实际上所有这些标志都可以被允许多次设置标志。

Would it be thread safe to instead change this to a static boolean? Set the flag to true by whoever gets to do that, in fact all of them might be allowed to set the flag several times over.

我关心的是阅读旗帜的人能够多快找到它?它是对的吗?或者他们可能没有做对吗?

What I am concerned about is how quickly those reading the flag will be able to spot it? Will it be right of? Or might them not get it right?

此外,使用经常查询的AtomicBoolean的性能有多大?

Also, how big is the performance hit of using an AtomicBoolean that is queried often?

另外,我应该考虑在AtomicBoolean上使用volatile布尔值,因为我几乎只会设置一次并读取它,并且可以原子地执行set操作(复制atomicBoolean中的代码用于getAndSet())

Also, should I perhaps consider using a volatile boolean over AtomicBoolean since I will pretty much only be setting it once and reading it, and can perform the set operation atomically ( copying the code in AtomicBoolean for getAndSet())

推荐答案

当大多数线程只读取变量时,只读取其中一个并且一次将改变你不需要担心性能的价值。 AtomicBoolean正在使用一种名为 CAS 的机制,现在大多数现代处理器都支持这种机制,低水平的操作。使用CAS的结果是,在读取值时几乎没有任何缺点(这与使用标准锁不同)。使用你描述的方案volatile静态布尔就足够了 - volatile可以防止jvm在从变量读取时进行优化,比如重用寄存器中保存的值而不是检查内存中的值,所以每当一个线程改变变量的值时,其他线程就会看到改变。在您的情况下,两种解决方案都会提供相似但性能相同的结果。
在很多写操作的情况下,Volatile会比AtomicBoolean快,但说实话,我无法想象你有很多写作的情况,没有人有兴趣阅读。

When most of the threads only reads the variable and only one of them and once will change the value you don't need to worry about the performance. AtomicBoolean is using a mechanism called CAS which now is supported by most modern processors and is a low level operation. As a result of using CAS there is almost no drawback when reading the value (which is something different from using a standard lock). With the scenario you described volatile static boolean will be enough - volatile prevents jvm from doing optimization when reading from variable, like reusing value held in register instead of checking the value in memory, so whenever a thread change the value of variable other threads will see the change. In your case both solutions will give similar if not the same performance results. Volatile will be faster than AtomicBoolean in a scenario where lot of write operation have place, but honestly I can not imagine a scenario where you have lot of writing and no one is interested in reading.

这篇关于AtomicBoolean,设置标志一次,必要吗?静态布尔值可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆