...
Noncompliant Code Example (Volatile)
Declaring flag
volatile does not also fails to solve the problem either:
Code Block | ||
---|---|---|
| ||
final class Flag { private volatile boolean flag = true; public void toggle() { // Unsafe flag ^= true; } public boolean getFlag() { // Safe return flag; } } |
This code remains unsuitable for multithreaded use because declaring a variable volatile does not fails to guarantee the atomicity of compound operations on the variable.
...
This solution guards reads and writes to the flag
field with a lock on the instance, that is, this
. Furthermore, doing so synchronization ensures that changes are visible to all the threads. Now, only two execution orders are possible, one of which is shown in the following scenario:
...
The simple replacement of the two int
fields with atomic integers , in this example, fails to eliminate the race condition because the compound operation a.get() + b.get()
is still non-atomic.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8e5884db3e806a21-d54f3833-4ff04156-8b2c89b7-973717c5b8c8b4133db4d49f"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | Class | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="aa1b6f7760aa7597-39fe0bda-401845f4-9b0688f5-07851cd9c2f21e1dbb34cf3d"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 66. Synchronize access to shared mutable data | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="213a03ed7fa03572-45b0571b-4ec247af-965ba142-3c108246e6403c214340129a"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz 06]] | 2.3, Locking | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="219f2b4ada8ffaae-0b252c60-40da46e9-bd8ea209-208e5280cde1a0125f1ab961"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 17, Threads and Locks, | http://java.sun.com/docs/books/jls/third_edition/html/memory.html], ]]></ac:plain-text-body></ac:structured-macro> | |
| §17.4.5, Happens-Before Order | ||||
| §17.4.3, Programs and Program Order | ||||
| §17.4.8, Executions and Causality Requirements | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8c7a81e7c89ad892-b9462202-4b124a96-a3299b95-caa155a15bb87f83534694c1"><ac:plain-text-body><![CDATA[ | [[Lea 2000 | AA. Bibliography#Lea 00]] | Section 2.2.7, The Java Memory Model | ]]></ac:plain-text-body></ac:structured-macro> | |
| Section 2.1.1.1, Objects and Locks | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fef620e6eb97b61c-4614e48e-4b2741be-a5a1a0f3-5683db6c55303f477efc2c5c"><ac:plain-text-body><![CDATA[ | [[Tutorials 2008 | AA. Bibliography#Tutorials 08]] | [Java Concurrency Tutorial | http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html] | ]]></ac:plain-text-body></ac:structured-macro> |
Issue Tracking
...
...