 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between ReentrantLock and Synchronized in Java
There are two ways to get a lock on the shared resource by multiple threads. One is a Reentrant Lock (or read/write lock), and the other is by using the Synchronized method.
The reentrant lock class has been provided in the Java concurrency package from Java 5.
It is the implementation of the Lock interface, and according to Java docs, the implementation of the Lock interface provides more extensive operation than can be obtained using synchronized method.
What is the Reentrant lock?
ReetrantLock is a class that implements the Lock Interface. It provides the synchronization feature with great flexibility, which is why it is the most used lock class in Java.
Syntax:
ReentrantLock nameOflock = new ReentrantLock(); // by default false Or, ReentrantLock nameOflock = new ReentrantLock(true); // we can make it true
Example
The following is an example of a reentrant lock in Java:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ReentrantLockExample implements Runnable { private Lock lock = new ReentrantLock(); @Override public void run() { try { lock.lock(); // Acquiring the lock // Lock some resource System.out.println(Thread.currentThread().getName() + " is locking the resource."); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); // Releasing the lock } } public static void main(String[] args) { ReentrantLockExample example = new ReentrantLockExample(); Thread thread1 = new Thread(example); Thread thread2 = new Thread(example); thread1.start(); thread2.start(); } } The output of the above Java program is:
Thread-0 is locking the resource. Thread-1 is locking the resource.
Synchronized Java Keyword
Synchronized means an object having a synchronized method allows only one thread to access the synchronized method of code at the same time. So one thread cannot be read while another thread is updating it.
Example
The following is an example of SynchronizedLock in Java:
public class SynchronizedLockExample implements Runnable { // Declare the resource object private final Object resource = new Object(); @Override public void run() { synchronized (resource) { // Lock some resource System.out.println(Thread.currentThread().getName() + " is locking the resource."); // Add the critical section code here } } public static void main(String[] args) { SynchronizedLockExample example = new SynchronizedLockExample(); Thread thread1 = new Thread(example); Thread thread2 = new Thread(example); thread1.start(); thread2.start(); } } The output of the above Java program is:
Thread-0 is locking the resource. Thread-1 is locking the resource.
Difference between ReentrantLock and Synchronized
The following table shows the difference between ReentrantLock and Synchronized:
| Sr. No. | Key | ReentrantLock | Synchronized | 
|---|---|---|---|
| 1 | Acquire Lock | Reentrant lock class provides lock() methods to get a lock  on the shared resource by thread | You need to just write synchronized keyword to acquire a lock | 
| 2 | Release Lock | To release lock , programmers have to call unlock() method | It is done implicitly | 
| 3 | Ability to interrupt | lockInterruptibly() method can be used to interrupt the thread | There is no way to interrupt the thread | 
| 4 | Fairness | Constructor of this class has fairness parameter. If it is set to true then locks favor granting access to the longest-waiting * thread | Lock does not guarantee any particular  access orde | 
| 5 | Lock Release Order | Lock can be released in any order | Lock should be released in the same order in which they were acquired | 
