AtomicBoolean,设置标志一次,neccesary?可以静态布尔是否确定? [英] AtomicBoolean, set flag once, neccesary? Might a static boolean be ok?

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

问题描述

我设置一个由任何线程设置一次的标志。所有其他线程将在不同的时间,经常读这个标志反复。

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,因为我几乎只设置一次并读取它,并可以原子执行设置操作对于getAndSet()的AtomicBoolean中的代码)

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 static boolean将足够 - 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,设置标志一次,neccesary?可以静态布尔是否确定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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