JUC - Thread Synchronization Lock (Introduction to Lock Handling Mechanism)

Introduction to Lock Handling Mechanism

The core problem solved by juc's development framework is concurrent access and data security operation. When concurrent access is performed, if the lock is not properly controlled, it will cause blocking problems such as deadlock.

In order to solve such defects, juc has been redesigned for the concept of locks.

 

JUC lock mechanism

The JUC lock mechanism includes the following components:

  • Core interface: Lock , ReadWriteLock ;
  • AQS abstract class:
    • AbstractOwnableSynchronizer (exclusive lock);
    • AbstractQueuedSynchronizer (provides a framework for implementing blocking locks and related synchronizers (semaphores, events, etc.) that rely on first-in-first-out (FIFO) wait queues);
    • AbstractQueuedLongSynchronizer (64-bit synchronizer) 
  • Tools:
    • Reentrantock mutex lock, ReadWriteLock read- write lock, Condition control queue
    • LockSupport blocking primitive, Semaphore semaphore , CountDownLatch latch
    • CyclicBarrier fence, Exchanger switch, CompletableFuture thread callback

The fundamental reason why a series of lock processing tool classes are re-provided in juc ​​is that although Java's original lock mechanism (synchronized) can provide a safe access mechanism for data, its shortcomings are also very obvious: all A thread object can only enjoy one lock .

java.util.concurrent lock overview

  • java.util.concurrent.locks provides basic support for locks;
  • Lock interface: supports locking rules with different semantics (reentrancy, fairness, etc.)
    • Different semantics means that the lock can have "fair mechanism lock", "unfair mechanism lock", "reentrant lock", etc.;
      • Fairness mechanism: The mechanism by which different threads acquire locks is fair;
      • Unfair mechanism: It means that the mechanism by which different threads acquire locks is unfair;
      • Reentrant lock: It means that the same lock can be acquired multiple times by a thread. The biggest function of reentrant lock is to avoid deadlock.
  • The ReadWriteLock interface is similar to Lock and defines some locks that readers can share and write to;
  • The Condition interface describes condition variables that may be related to locks (similar to the use of the wait() method of the Object class).

 

Fair lock core concept

  • AbstractQueuedSynchronizer : It is an abstract class that manages "locks" in Java. Many public methods of locks are implemented in this class. AbstractQueuedSynchronizer is an exclusive lock (eg, ReentrantLock).
  • AbstractQueuedSynchronizer category:
    • Exclusive lock: The lock can only be occupied by one thread lock at the same point in time. According to the lock acquisition mechanism, it is divided into "fair lock" and "unfair lock". The fair lock is to acquire the lock fairly according to the first-come-first-served rule by waiting for the thread through the CLH; when the non-fair lock acquires the lock, it will ignore the CLH waiting queue and go back to the lock directly.
    • Shared lock: A lock that can be owned by multiple threads at the same time and can be shared.
  • CLH queue (Craig, Landin, and Hagersten locks): CLH lock is also a scalable, high-performance, and fair spin lock based on linked list. The application thread only spins on local variables, and it continuously polls the status of the predecessor. The spin is ended if the predecessor is found to have released the lock.
  • CAS method (Compare And Swap): Compare and exchange method, it is an atomic operation method: that is, the data operated by CAS is performed in an atomic manner.

CLH lock - solve deadlock problem

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324959088&siteId=291194637