A synchronized method in Java is a thread-safe construct declared with theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
synchronized keyword, which enforces mutually exclusive access to the method’s execution block. It guarantees that only one thread can execute the method at a time for a specific monitor lock, establishing a happens-before relationship that ensures memory visibility and prevents thread interference.
Syntax
Lock Mechanics and Monitor Targets
Java utilizes intrinsic locks (also known as monitor locks) built into every object to manage synchronization. The target of the monitor lock depends on the method’s context:- Instance Synchronized Methods: The thread acquires the intrinsic lock on the current object instance (
this). If Thread A is executing a synchronized instance method, Thread B is blocked from entering any synchronized instance method on that exact same object until Thread A releases the lock. - Static Synchronized Methods: Because static methods belong to the class rather than an instance, the thread acquires the intrinsic lock on the
Classobject associated with the class (e.g.,MyClass.class). This lock is distinct from any instance locks; a thread executing a static synchronized method will not block another thread executing an instance synchronized method on an object of the same class.
Lock Acquisition and Release
- Acquisition: The lock is acquired implicitly when the thread invokes the method. If the lock is held by another thread, the invoking thread enters the
BLOCKEDstate and waits for the monitor to become available. - Release: The lock is released implicitly when the method execution completes. This occurs whether the method terminates normally (via a
returnstatement or reaching the end of the block) or abruptly (by throwing an unhandled exception).
Reentrancy
Java’s intrinsic locks are reentrant. If a thread already holds the monitor lock for a specific object or class, it can freely enter other synchronized methods that require the same lock without deadlocking itself. The JVM maintains a hold count for the lock; it increments when the thread re-enters a synchronized method and decrements when it exits. The lock is fully released to other threads only when the hold count reaches zero.Bytecode Implementation
Unlike synchronized blocks, which use explicitmonitorenter and monitorexit bytecode instructions, synchronized methods are handled implicitly at the method invocation level.
When the Java compiler compiles a synchronized method, it sets the ACC_SYNCHRONIZED flag in the method’s method_info structure, which is located in the methods table of the ClassFile structure. When the JVM invokes a method, it checks this flag. If ACC_SYNCHRONIZED is set, the executing thread automatically attempts to acquire the appropriate monitor lock before executing the method’s bytecode instructions.
Master Java with Deep Grasping Methodology!Learn More





