ReadWriteLock in Java

A ReadWriteLock interface in Java is more sophisticated than the Lock interface. Imagine you have an application that reads from and writes to some resources, but reading frequency is much more higher than writing frequency. Two or more threads reading the same resource do not cause any problem for each other, i.e., multiple threads reading the same resource are granted access at the same time. But, if a single thread wants to write to the resource then neither reads nor writes must be in progress at the same time. To…

Continue

ReentrantLock in Java

A Reentrant lock is an implementation of java.util.concurrent.Lock interface with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities. Lock is acquired by lock() method and held by Thread until a call to unlock() method. ReentrantLock provides same visibility and ordering guarantee, provided by implicit locking mechanism(synchronized), which means, unlock() happens before another thread gets lock().

Continue

Lock in Java

Introduction You may be already aware of basic concepts around thread synchronization and various mechanisms using synchronized keyword. A lock interface is a thread synchronization aid like synchronized block except lock can be used in a more sophisticated way than Java’s synchronized blocks. A lock is a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be…

Continue