You can ask whether the current thread holds the lock on a given object by passing that object to the Thread
class's static holdsLock method, which returns true if the current thread does hold the lock on that object.
This is typically used to assert that a lock is held when needed. For example, a private method that expects to
be invoked only from synchronized public methods might assert that fact:
assert Thread.holdsLock(this);
You will also occasionally find it important that a certain lock not be held in a method. And in some rare
circumstances you might want to choose whether or not to acquire a given lock depending on whether or not
another lock is held.
When an extended class overrides a synchronized method, the new method can be synchronized or
not. The superclass's method will still be synchronized when it is invoked. If the unsynchronized method
uses super to invoke the superclass's method, the object's lock will be acquired at that time and will be
released when the superclass's method returns. Synchronization requirements are a part of the implementation
of a class. An extended class may be able to modify a data structure such that concurrent invocations of a
method do not interfere and so the method need not be synchronized; conversely the extended class may
enhance the behavior of the method in such a way that interference becomes possible and so it must be
synchronized.
14.3.2. Static synchronized Methods
Static methods can also be declared synchronized. Associated with every class is a Class objectsee
"The Class Class" on page 399. A static synchronized 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 synchronized 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
}
The expression expr must evaluate to an object reference. After the lock is obtained, the statements in
the block are executed. At the end of the block the lock is releasedif an uncaught exception occurs within the
block, the lock is still released. A synchronized method is simply a syntactic shorthand for a method
whose body is wrapped in a synchronized statement with a reference to this.