Sunday, June 9, 2019

Lock Interface In Concurrency API

Q- What is lock in java?
A lock is a thread synchronization mechanism like synchronized blocks except locks can be more sophisticated than Java's synchronized blocks.

Lock interface:  Lock interface is present in java.util.concurrent.locks package. ReentrantLock class implements the Lock interface.
Lock implementations provide more extensive locking (means only one thread can read or write) operations which is like using synchronized methods and statements. Only one lock can be acquired at the same time either read or write.
Note:- Use of ReadWriteLock : Allow concurrent access to a shared resource, such as the read lock of a ReadWriteLock and write is extensive.
public interface Lock

All Known Implementing Classes:
ReentrantLock, 
ReentrantReadWriteLock.ReadLock, 
ReentrantReadWriteLock.WriteLock
ReentrantLock Class : ReentrantLock is a implementation class of Lock interface and provides synchronization to methods while accessing shared resources. ReentrantLock class allows a thread to lock a method even if it already have the lock on other method.
Lock interface has below methods. 
  • void lock()
  • void unlock()
  • boolean tryLock() 
  • boolean tryLock(long timeout, TimeUnit timeUnit) 
  • void lockInterruptibly() 
  • getHoldCount():
  • isHeldByCurrentThread():
public class ReentrantLock  extends Object implements Lock, Serializable

All Known Implementing Classes:
ReentrantReadWriteLock
  ReadWriteLock interface :  ReadWriteLock  is an interface. It maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.
  • Lock readLock() – returns the lock that’s used for reading
  • Lock writeLock() – returns the lock that’s used for writing
public interface ReadWriteLock

All Known Implementing Classes:
ReentrantReadWriteLock
ReentrantReadWriteLock Class: ReentrantReadWriteLock class implements the ReadWriteLock interface. This lock allows both readers and writers to reacquire read or write lock.
We can use a fair ordering policy in ReentrantReadWriteLock. Bydefault fair ordering policy is false.
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
  • Read Lock – if no thread acquired the write lock or requested for it then multiple threads can acquire the read lock
  • Write Lock – if no threads are reading or writing then only one thread can acquire the write lock
public class ReentrantReadWriteLock
extends Object
implements ReadWriteLock, Serializable

All Implemented Interfaces:
Serializable, ReadWriteLock
ReadWriteLock lock = new ReentrantReadWriteLock();
Lock writeLock = lock.writeLock();
Lock readLock = lock.readLock();

  StampedLock :  StampedLock was introduced in Java 8. It also supports read and write locks. However, lock acquisition methods return a stamp that will be used to release a lock or to check if the lock is still valid:
public class StampedLock
extends Object
implements Serializable
It also supports both read and write locks. It is a capability-based lock with three modes for controlling read/write access. The state of a StampedLock consists of a version and mode.
  • Writing.
  • Reading.
  • Optimistic Reading.
  Conditions : Condition is alternative way of Object monitor methods like wait, notify and notifyAll into distinct objects using synchronized keyword.
A Condition instance is intrinsically bound to a lock. To obtain a Condition instance for a particular Lock instance use its newCondition() method.
Below are method used in condotion. 
  • void await()
  • boolean await(long time, TimeUnit unit)
  • long awaitNanos(long nanosTimeout)
  • void awaitUninterruptibly()
  • boolean awaitUntil(Date deadline)
  • void signal()
  • void signalAll()
await() is like wait() method , signal() like notify and signalAll like notifyAll

public interface Condition

All Known Implementing Classes:
AbstractQueuedLongSynchronizer.ConditionObject,
 AbstractQueuedSynchronizer.ConditionObject
The Condition class provides the ability for a thread to wait for some condition to occur while executing the critical section.
ReentrantLock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition1 = lock.newCondition();

Note:

  • If you have a read lock, you can not acquired a write lock.
  • If you have a write lock you can not acquired read lock.

Related Tutorials

No comments:

Post a Comment