
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
57 lines
1.5 KiB
Plaintext
57 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Marking an array ``++volatile++`` means that the array itself will always be read fresh and never thread cached, but the items _in_ the array will not be. Similarly, marking a mutable object field ``++volatile++`` means the object _reference_ is ``++volatile++`` but the object itself is not, and other threads may not see updates to the object state.
|
|
|
|
|
|
This can be salvaged with arrays by using the relevant AtomicArray class, such as ``++AtomicIntegerArray++``, instead. For mutable objects, the ``++volatile++`` should be removed, and some other method should be used to ensure thread-safety, such as synchronization, or ThreadLocal storage.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
private volatile int [] vInts; // Noncompliant
|
|
private volatile MyObj myObj; // Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
private AtomicIntegerArray vInts;
|
|
private MyObj myObj;
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/x/UzdGBQ[CERT, CON50-J.] - Do not assume that declaring a reference volatile guarantees safe publication of the members of the referenced object
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
* Use an "Atomic[Reference|Integer|Long]Array" instead.
|
|
* Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
volatile [ type ]
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 17 Jun 2015, 14:45:38 Nicolas Peru wrote:
|
|
looks good
|
|
|
|
endif::env-github,rspecator-view[]
|