Sunday, November 08, 2015

Atomicity, Visibility & Ordering

Atomicity

If an operation comprising of set of actions is either all done or not at all i.e. it is indivisible, then its an atomic operation. 
To ensure atomicity:

  • Use atomic types defined in java.util.concurrent.atomic package that support atomic operations on single variables
  • Use synchronized blocks or methods to wrap all steps of an operation


Visibility

Visibility deals with whether changes made to a shared object/value are visible to other threads or not.

The compiler may optimize the access to a variable by storing it in the cache for the thread and thus any writes to the variable from another thread are not visible.

To ensure visibility:

  • The variable can be declared as volatile which forces the compiler to always read/write variable value from memory
  • Access the variable from within a synchronized block thus making sure that the compiler reads/writes the variable from memory when entering/leaving the synchronized block.


Ordering

Ordering refers to the order of execution of discrete instructions of an operation. Specifically in a multi-threaded environment, the manner of interleaving of instructions could lead to unexpected outcomes. This is referred to as race-condition.

Refer to the below article for more details on these topics: