Java Reference
In-Depth Information
method need not be synchronized; conversely the extended class may
enhance the behavior of the method in such a way that interference be-
comes possible and so it must be synchronized.
14.3.2. Static
synchronized
Methods
Static methods can also be declared
synchronized
. Associated with every
chronized method acquires the lock of the
Class
object for its class.
Two threads cannot execute static synchronized methods of the same
class at the same time, just as two threads cannot execute synchronized
methods on the same object at the same time. If static data is shared
between threads then access to it must be protected by static synchron-
ized methods.
Acquiring the
Class
object lock in a static synchronized method has no
effect on any objects of that class. You can still invoke synchronized
methods on an object while another thread holds the
Class
object lock in
a static synchronized method. Only other static synchronized methods
are blocked.
14.3.3.
synchronized
Statements
The
synchronized
statement enables you to execute synchronized code
that acquires the lock of any object, not just the current object, or for
durations less than the entire invocation of a method. The
synchronized
statement has two parts: an object whose lock is to be acquired and a
statement to execute when the lock is obtained. The general form of the
synchronized
statement is
synchronized (
expr
) {
statements
}