The
synchronized keyword can be used either as a method modifier or as a standalone statement with a block of code. It executes code while holding the
monitor of an object. When using as a statement, the target object is specified explicitly. As a method modifier,
this is used for non-static methods and the
.class of the current class for static methods.
It is one of the primary tools for ensuring thread safety. No more than one
synchronized block on the same object can execute at the same time. This type of lock is known as an
intrinsic lock or
monitor lock and is discussed in detail in
Java Concurrency in Practice 2.3.1.
A
synchronized block is also required for using the methods
wait, notify, and notifyAll.