Posts

Showing posts with the label Concurrent

Quick Refresh : Lock in Java : ReentrantLock, ReentrantReadWriteLock, StampedLock and Conditions

1. Lock a lock is a more flexible and sophisticated thread synchronization mechanism than the standard  synchronized  block. The  Lock  interface has been around since Java 1.5. It’s defined inside the   java.util.concurrent. locks  package  and it provides extensive operations for locking.   2. Differences between  Lock  and  Synchronized   block There are few differences between the use of synchronized  block  and using  Lock  API’s: A  synchronized   block  is fully contained within a method –  we can have  Lock  API’s  lock()  and  unl ock()  operation in separate methods A s ynchronized block  doesn’t support the fairness, any thread can acquire the lock once released, no preference can be specified.  We can achieve fairness within the  Lock  APIs by specifying the  fairness  property . It makes sure that longes...