Javatpoint Logo
Javatpoint Logo

Lock Framework Vs. Thread Synchronization in Java

Lock Framework

In Java, lock framework and thread synchronization mechanisms are used to manage concurrent access to shared resources and ensure thread safety in multithreaded applications.

It is a set of classes and interfaces, and it is present in java.util.concurrent package. It provides a flexible and efficient way to synchronize access to shared resources. It also provides a number of concrete lock implementations. The most common concrete lock implementations are the ReentrantLock and the ReadWriteLock.

The ReentrantLock class is a type of lock in Java that allows a thread to acquire the lock multiple times without blocking itself. It is a recursive lock and provides methods for acquiring the lock fairly or unfairly.

The ReadWriteLock class provides a mechanism for multiple threads to read a shared resource simultaneously while ensuring that only one thread can write to the resource at any given time.

The lock framework is a powerful tool that regulates access to shared resources in multithreaded applications, improving performance and scalability by preventing conflicts and ensuring orderly execution.

Here's an example of how to use the lock framework.

LockExample.java

Output:

Thread-1 incremented counter to 1
Thread-0 incremented counter to 2
Thread-1 incremented counter to 3
Thread-0 incremented counter to 4

Thread Synchronization

Thread synchronization in Java is a mechanism that ensures that multiple threads can access shared resources or perform operations in a mutually exclusive manner. It prevents threads from interfering with each other, leading to data inconsistency or race conditions.

There are the following ways to achieve thread synchronization in Java:

1. Synchronized methods: By declaring a method as synchronized, only one thread can execute that method at a time. Other threads that try to access the synchronized method will be blocked until the executing thread completes its execution.

SynchronizedMethodExample.java

Output:

Thread Thread 1 - Step 1Thread Thread 1 - Step 2
Thread Thread 1 - Step 3
Thread Thread 1 - Step 4
Thread Thread 1 - Step 5
Thread Thread 2 - Step 1Thread Thread 2 - Step 2
Thread Thread 2 - Step 3
Thread Thread 2 - Step 4
Thread Thread 2 - Step 5

2. Synchronized Blocks: Instead of synchronizing the entire method, specific blocks of code can be synchronized using the synchronized keyword. It allows for finer-grained control over which parts of the code need synchronization.

3. ReentrantLock: More flexible thread synchronization. Allows multiple threads to acquire and release locks in a controlled way.

ReentrantLockExample.java

Output:

Thread 1: Lock acquired
Thread 1: Lock released
Thread 2: Lock acquired
Thread 2: Lock released

4. A semaphore is a variable or abstract data type that limits the number of threads that can access a shared resource. Semaphores are used to control access to a common resource by multiple threads in a concurrent system, such as a multitasking operating system.

ShortSemaphoreExample.java

Output:

Thread 1 is waiting to acquire a permit.
Thread 2 is waiting to acquire a permit.
Thread 1 has acquired a permit and is working.
Thread 1 has finished working and is releasing the permit.
Thread 2 has acquired a permit and is working.
Thread 2 has finished working and is releasing the permit.

Difference Between the Lock Framework and Thread Synchronization in Java

Lock Framework Thread Synchronization
Introduced in Java 5 Available since the early version of Java
Provides enhanced flexibility and precise control over locking mechanisms. Provides a simpler and more direct method to achieve synchronization.
Support multiple conditions and fairness. Supports only basic synchronization.
Allows non-blocking and timed acquisition of locks. Allows blocking the acquisition of locks.
Allows multiple locks to be acquired and released in any order. Requires acquiring and releasing locks in a specific order.
Provides better performance in highly contended scenarios. May have performance overhead in highly contended scenarios.
It can be more complex to use and understand. Simpler to use and understand.
Requires explicit acquisition and release of locks. Automatically acquires and releases locks.
Supports ReentrantLock, ReentrantReadWriteLock, etc. Supports synchronized keyword, wait(), notify(), notifyAll() methods.

Overall, the lock framework and thread synchronization in Java are essential for maintaining thread safety and preventing race conditions in multithreaded applications. They help in ensuring that concurrent access to shared resources is properly synchronized, leading to correct and predictable program behavior.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA