...
- A write to a variable does not depend on its current value.
- The write is not involved with writes of other variables.
- Only a single thread ever updates the value.
- Locking is not required for any other reason.
Synchronizing the code makes it easier to reason about the behavior of the code and is frequently, a more secure approach than using volatile
, however. However, it is slightly more expensive . Excessive use of synchronization can also lead to deadlocksand can cause deadlocks when used excessively.
Declaring a variable as volatile or correctly synchronizing the code guarantees that 64-bit primitive variables of type long
and double
are accessed atomically (see CON25-J. Ensure atomicity when reading and writing 64-bit values for information on sharing long
and double
variables among multiple threads).
...
This noncompliant code example uses a shutdown()
method to set a non-volatile done
flag that is checked in the run()
method. If one thread invokes the shutdown()
method to set the flag, it is possible that another thread might not observe this change. Consequently, the second thread may still observe that done
is false
and incorrectly invoke the sleep()
method.
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private boolean done = false; public void run() { while (!done) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // handle exception } } } protectedpublic void shutdown() { done = true; } } |
If one thread invokes the shutdown()
method to set the flag, it is possible that another thread might not observe this change. Consequently, the second thread may observe that done
is still false
and incorrectly invoke the sleep()
method.
Compliant Solution (volatile
)
In this This compliant solution , declares the done
flag is declared as volatile so that updates are visible to other threads.
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private volatile boolean done = false; public void run() { while (!done) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // handle exception } } } protectedpublic void shutdown() { done = true; } } |
...
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private AtomicBoolean done = new AtomicBoolean(false); public void run() { while (!done.get()) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // handle exception } } } protectedpublic void shutdown() { done.set(true); } } |
...
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private boolean done = false; public void run() { while (!isDone()) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // handle exception } } } protectedpublic synchronized boolean isDone() { return done; } protectedpublic synchronized void shutdown() { done = true; } } |
...