开发者

Is it necessary to declare an AtomicReference as volatile?

The same for all other atomic objects? It's easier to explain the question for an AtomicInteger. Since more then 1 thread are accessing the reference to myInt, isn't it possible that one thread sees the registered cached value, for example null, for this object开发者_如何学JAVA, unless it is also declared volatile? If not how come?


Not only is it not necessary, it's actually semantically wrong. AtomicReference holds the "real" reference within itself, and manages access to it using its own synchronization constructs. The JVM's own synchronization constructs (synchronized, volatile, etc) and not used. The AtomicReference object itself should not be treated as volatile. If anything, consider making it final.

Also consider this question - volatile can be considered to be an alternative to using AtomicReference, if all you need is get and set operations.


The "atomic" objects are not immutable, so they should be thread safe only if they are published properly. For example, when you do something like this, you will need to use the volatile keyword.

volatile AtomicInteger counter = // initialize counter

int harvest(){
    AtomicInteger old = counter;
    counter = new AtomicInteger();
    return old.get();
}

If you remove the volatile from the above code, you could indeed lose some increments. According to the spec, you might also get a reference to an AtomicInteger object that is not fully constructed, and thus get undefined behavior.

So, do you need to declare your atomic object as volatile? The answer is it depends. They are only thread safe as long as they are published properly, as any other thread safe objects are (except immutable objects, which are special case). In most cases, you should make them final.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜