Types of Locks
About
Locks in Java help synchronize access to shared resources in multithreaded environments. They prevent race conditions and ensure data consistency. There are multiple types of locks based on their scope, usage, and implementation.
1. Object-Level Lock
An object-level lock is associated with a specific instance of a class. It ensures that only one thread can execute a synchronized method/block on a given object at a time.
Example
If two threads call
display()
on the same object, they will execute one after another.If two threads call
display()
on different objects, they will execute independently.
Equivalent Using Explicit Lock
2. Class-Level Lock (Static Synchronization)
A class-level lock is used to synchronize static methods or blocks. It prevents multiple threads from executing static methods of the same class simultaneously.
Affects all instances of the class.
Only one thread can execute any synchronized static method at a time.
Example
Equivalent Using Explicit Lock
3. Method-Level Lock
A method-level lock is applied by synchronizing an entire method. It applies object-level or class-level locking, depending on whether the method is instance or static.
Example (Instance Method)
Example (Static Method)
4. Block-Level Lock
A block-level lock restricts synchronization to a specific block inside a method instead of locking the entire method.
Example (Object-Level)
Example (Class-Level)
5. Reentrant Lock
A Reentrant Lock allows the same thread to acquire the same lock multiple times without causing a deadlock.
Example
Unlike
synchronized
,ReentrantLock
provides:Try Locking (
tryLock()
)Interruptible Locking (
lockInterruptibly()
)Fair Locking (
new ReentrantLock(true)
)
6. Fair and Unfair Locks
Fair Lock: Threads are scheduled in the order they requested the lock.
Unfair Lock (default): The lock may not follow strict ordering.
Example
Fair locks prevent thread starvation, but can reduce performance.
7. Read-Write Lock
A ReadWriteLock allows:
Multiple readers to access a resource concurrently.
Only one writer to access the resource exclusively.
Useful for caching and database operations.
Example
8. Stamped Lock
A StampedLock improves upon ReadWriteLock
by:
Optimistic Reads (reading without blocking writes).
Explicit stamp validation.
Provides better performance than
ReadWriteLock
Example
9. Spin Lock
A Spin Lock makes threads actively wait (busy-waiting) instead of blocking.
Used in low-latency applications but wastes CPU cycles.
Example
Comparison
Object-Level
Instance
Prevent multiple threads from modifying an object
Simple
Blocks entire method
Class-Level
Static Methods
Synchronize across all instances
Ensures global lock
Affects all threads
Method-Level
Entire Method
Prevent concurrent execution of a method
Easy to use
Can be inefficient
Block-Level
Inside a Method
Synchronize specific part of a method
More efficient
Requires careful implementation
ReentrantLock
Custom
More control over locking
Advanced features
Requires manual unlocking
ReadWriteLock
Read-Write
Allows concurrent reads
Faster reads
Complexity
StampedLock
Read-Write
Optimistic reads for performance
High efficiency
Difficult to implement
Spin Lock
CPU-bound
High-performance, low-latency apps
No context switching
Wastes CPU
Last updated
Was this helpful?