Java Memory Model


Source: JSR 133 (Java Memory Model) FAQ


It seems to understand the memory model at a deep layer we must understand a few concepts and mechanisms.

It seems as though reads and writes to fields are batched up in sets in local processor caches per lexical
execution scope that are flushed to main memory on volatile reads or monitor releases. It seems every
variable in a java program is in main memory at some point and thus available to be read by any thread.


  • Compiler
  • Memory
  • local processor cache
  • Main Memory
  • Monitor
  • Reads
  • Writes
  • Ordering
  • Reordering
  • Synchronization
  • Threads
  • Memory visibility
  • Synchronized Block
  • volatile field
    • special fields used for communicating state between threads. Each read will see the
      last write by any thread.
    • writing to a volatile field has the same effect as a monitor release.
    • reading from a volatile field has the same memory affect as a monitor acquire.
  • data race
  • final
    • ensures, in the new memory model, accessed fields the value set at time of creation.
      However, synchronization must still be used to ensure objects with final fields are
      correctly seen by other threads. Why though?


  • Acquiring a monitor
  • Releasing a monitor
  • Read field
  • write field
  • lock
  • unlock
  • start
  • join

Ordering rules

  • Each action in a thread occurs prior to every action of a subsequent thread occurring in a program.
  • An unlock on a monitor occurs prior to any subsequent lock on that same monitor.
  • A write to a volatile field occurs prior to any subsequent read of that same volatile field.
  • A call to start() on a thread occurs prior to any actions occurring in the started thread.
  • All actions occurring in a thread occur prior to any other thread successfully returning from a join() on that thread.


  • There is no defined behavior if you want to use JNI to change final fields.
  • for proper happens-before semantics between two threads each thread must execute release and acquire actions on the
    same field.