Synchronized

The synchronized keyword has two principal functions in Java:

Mutual exclusion with synchronized

The first purpose of synchronized is as a mutual exclusion lock (mutex). A synchronized block is always synchronized on a particular object. When a block of code is declared synchronized, then at any one time, only one thread can be "inside" any block synchronized on that object. For example, in the following code:

synchronized (someObject) {
  // ... do some stuff ...
}

once one thread is inside this block, any other thread wanting to enter a block synchronized on someObject will have to wait until the first thread exits the block.

In other words, someObject acts as a kind of lock.

Methods can also be declared synchronized. In this case, the block is still synchronized on an object:

Type of methodObject synchronized on
SynchronizedClass object of the class that the method belongs to.
Non-synchronizedInstance of the class (i.e. the "actual object") that the method is called on.

Visibility

Subtly, synchronized— or at least, some form of synchronization— is generally necessary in any case where different threads will access some shared data, whether or not they need to access it simultaneously. The reason is that, on many architectures, data written by a given thread may not actually be written to main memory, or otherwise made available to other threads, unless the processor is specifically told to make such a guarantee. Using synchronized makes that guarantee: the JVM makes sure, on exiting a synchronized block, that data written by that thread (before exiting the block) will be made available to any other thread that subsequently synchronizes on the same object.

Typical uses of synchronized

The synchronized keyword is used in a variety of cases where data is accessed by more than one thread.

Further information about synchronized