Reentrant Synchronization – Concurrency: Part I

Reentrant Synchronization

While a thread is inside a synchronized method of an object, all other threads that wish to execute this synchronized method or any other synchronized method of the object will have to wait in the entry set. This restriction does not apply to the thread that already has the lock and is executing a synchronized method of the object. Such a thread can invoke, directly or indirectly, other synchronized methods of the object without being blocked—that is, once a thread has a lock on an object, it can acquire the lock on the same object several times, called reentrant synchronization. The non-synchronized methods of the object can always be called at any time by any thread.

Examples of reentrant synchronization can be found in §23.4, p. 1460.

Synchronization on Class Lock

Static methods synchronize on the class lock. Acquiring and releasing a class lock by a thread in order to execute a static synchronized method is analogous to that of an object lock for a synchronized instance method. A thread acquires the class lock before it can proceed with the execution of any static synchronized method in the class, blocking other threads wishing to execute any static synchronized methods in the same class. The blocked threads wait in the entry set of the class lock. This does not apply to static, non-synchronized methods, which can be invoked by any thread. A thread acquiring the lock of a class to execute a static synchronized method has no effect on any thread acquiring the lock on any object of the class to execute a synchronized instance method. In other words, synchronization of static methods in a class is independent from the synchronization of instance methods on objects of the class.

A subclass decides whether the new definition of an inherited synchronized method will remain synchronized in the subclass.

Leave a Reply

Your email address will not be published. Required fields are marked *