Difference between synchronized and ReentrantLock in java






Before this tutorial please read :




There are following important differences between synchronized and ReEntrantLocks in java >



synchronized
ReentrantLock
Does not provide any fair locks in java.
provides fair locks, when lock is fair - first lock is obtained by longest-waiting thread in java.


Constructor to provide fairness -

ReentrantLock(boolean fair)
Creates an instance of ReentrantLock.
When fair is set true, first lock is obtained by longest-waiting thread.
If  fair is set false, any waiting thread could get lock, at discretion of implementation.
Does not provide tryLock() method or its functionality. Thread always waits for lock in java.
Provide tryLock() method. If lock is held by another thread then method return false in java.


boolean tryLock()
Acquires the lock if it is not held by another thread and returns true. And sets lock hold count to 1.
If current thread already holds lock then method returns true. And increments lock hold count by 1.
If lock is held by another thread then method return false.

There is no method for lock interruptibility, though current thread waits until one of the following thing happens -
  • The lock is acquired by the current thread, or
  • Some other thread interrupts the current thread in java.
void lockInterruptibly()
If current thread already holds lock then method returns true. And increments lock hold count by 1.
If the lock is held by another thread then the current thread waits until one of the following thing happens -
  • The lock is acquired by the current thread, or
  • Some other thread interrupts the current thread.
As soon as current thread acquires the lock it sets lock hold count to 1.

Does not provide any method to return number of threads that may be waiting to acquire this lock in java.
provide int getQueueLength() method to return number of threads that may be waiting to acquire this lock in java.
holdsLock() method is used to find out whether lock is held by current thread or not. If current thread holds lock method returns true.
isHeldByCurrentThread() method is used to find out whether lock is held by current thread or not. If current thread holds lock method returns true in java.
Thread can hold lock on object monitor only once.
if current thread already holds lock then lock hold count is increased by 1 when lock() method is called.

method to maintain lock hold count -
void lock()
Acquires the lock if it is not held by another thread. And sets lock hold count to 1.
If current thread already holds lock then lock hold count is increased by 1.
Does not provide any new condition() method.
provides newCondition() method.
Method returns a Condition instance to be used with this Lock instance.
Condition instance are similar to using Wait(), notify() and notifyAll() methods on object.

  • IllegalMonitorStateException is thrown if this lock is not held when any of the Condition waiting or signalling methods are called.

  • Lock is released when the condition waiting methods are called and before they return, the lock is reacquired and the lock hold count restored to what it was when the method was called in java.

  • If a thread is interrupted while waiting then InterruptedException will be thrown and following things will happen -
    • the wait will be over, and
    • thread's interrupted status will be cleared.

  • Waiting threads are signalled in FIFO (first in first out order) order.
  • When lock is  fair, first lock is obtained by longest-waiting thread.
If lock is not  fair, any waiting thread could get lock, at discretion of implementation in java.



So, in this thread concurrency tutorial we read what are differences between synchronized and ReEntrantLocks in java


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Executor framework >

Executor and ExecutorService framework in java



Thread concurrency Interviews >

No comments:

Post a Comment