“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

JUC Lock interface this article overall understanding of Lock interface, today we analyze and practice the application of Lock interface important implementation ReetrantLock and ReenTrantreadwrite elock.

ReetrantLock class diagram analysis

ReetrantLock is an exclusive, reentrant lock. An exclusive lock is one that only one thread can acquire and own. ReetrantLock The ReetrantLock includes fairReetrantLock and unfairReetrantLock. FairReetrantLock means that the mechanism for different threads to acquire the lock is fair, while unfairReetrantLock means that the mechanism for different threads to acquire the lock is unfair. Fair or unfair is whether multiple threads acquire locks in the order in which they apply for them. If so, it is fair; otherwise, it is unfair.

As shown in the UML class diagram above.

  • ReentrantLock implements the Lock interface.
  • In the already have a member variable sync, sync is of type sync, sync is an abstract class, it integrated the AQS (AbstractQueuedSynchronizer).
  • Within ReentrantLock there is fair Lock, the class FairSync; There is also unfair Lock, the L class NonFairSync. Both classes are subclasses of Sync, which means that ReentrantLock can only be used with either fair or unfair locks. The default is unfair locks.

ReentrantReadWriteLock implements class diagram analysis

ReentrantLock is an exclusive lock. ReentrantReadWriteLock is not an exclusive lock. Its read lock is a shared lock and its write lock is an exclusive lock. So ReentrantReadWriteLock allows multiple reader threads to access it at the same time. If one writer thread is present, all other reader threads are blocked. In most cases, there are more read scenarios than write scenarios, so read/write locks can provide better concurrency and throughput in scenarios where there are more reads than writes.

As shown in the UML class diagram above.

  • ReentrantReadWriteLock is an implementation class of ReadWriteLock
  • ReentrantReadWriteLock contains member variables sync, readLock, and writeLock. Sync is of type sync. Like ReentrantLock, ReentrantReadWriteLock is an abstract class inherited from AQS. The type of readLock is readLock, and the type of writeLock is writeLock. Both readLock and writeLock implement the Lock interface.

Three examples,

ReentrantLock

public class LockExample { private static final ReentrantLock FAIR_LOCK = new ReentrantLock(true); private static int i; public static void add(){ FAIR_LOCK.lock(); try { Thread.yield(); i++; } catch (Exception e){ throw new RuntimeException(e); }finally { FAIR_LOCK.unlock(); } } public static void unsafeAdd(){ i++; }}Copy the code

ReentrantReadWriteLock

public class ReadWriteLockExample { static final Map<String,String> CACHE = new HashMap<>(); static final ReentrantReadWriteLock RRW_LOCK = new ReentrantReadWriteLock(); static final Lock R_LOCK = RRW_LOCK.readLock(); static final Lock W_LOCK = RRW_LOCK.writeLock(); public static String get(String key){ R_LOCK.lock(); try{ return CACHE.get(key); } finally { R_LOCK.unlock(); } } public static void put(String key,String value){ W_LOCK.lock(); try{ CACHE.put(key,value); }finally { W_LOCK.unlock(); }}}Copy the code