Multithreaded programming (7)--an overview of the Juc lock framework

A brief description of the lock.

According to the time when the lock was added to Java, the locks in Java can be divided into "synchronized locks" and locks in the JUC package.

1.1 Synchronization lock

       A synchronization lock is a lock that synchronizes through the synchronized keyword to achieve mutually exclusive access to competing resources. Synchronized locks have been supported since Java 1.0.

  The principle of synchronization lock is that for each object, there is only one synchronization lock; different threads can access the synchronization lock together. However, at the same point in time, the synchronization lock can and only be acquired by one thread. In this way, the thread that has acquired the synchronization lock can be scheduled on the CPU to execute on the CPU; while the thread that has not acquired the synchronization lock must wait until the synchronization lock is acquired before continuing to run. This is the principle of multi-thread synchronization through synchronization locks!


1.2 Locks in the juc package

     Compared with synchronous locks, the locks in the JUC package are more powerful. It only appeared in Java 5. It provides a framework for locks that allows more flexible use of locks, but its usage is more difficult .     

  The locks in the JUC package include: Lock interface, ReadWriteLock interface, LockSupport blocking primitive, Condition condition, AbstractOwnableSynchronizer/AbstractQueuedSynchronizer/AbstractQueuedLongSynchronizer three abstract classes, ReentrantLock exclusive lock, ReentrantReadWriteLock read-write lock. Because CountDownLatch, CyclicBarrier and Semaphore are also implemented through AQS; UC locks and related classes and interfaces are mostly in java.util.concurrent.locks. Of course, there are also three classes CountDownLatch , CyclicBarrier , Semaphore in java.util.concurrent . These three classes are generally considered to be concurrent tool classes . Although they do not implement the Lock interface , I still tend to regard them as locks.

1.2.1 lock interface

    The Lock interface in the JUC package supports locking rules with different semantics (reentrancy, fairness, etc.). The so-called semantic difference means that the lock can have "fair mechanism lock", "unfair mechanism lock", "reentrant lock" and so on. "Fairness mechanism" means "the mechanism by which different threads acquire locks is fair", while "unfair mechanism" means "the mechanism by which different threads acquire locks is unfair", and "reentrant locks" refer to the same A lock can be acquired multiple times by a thread.

1.2.2 ReadWriteLock interface

    The ReadWriteLock interface defines, in a similar manner to Lock, some locks that can be shared by readers and exclusive by writers. There is only one class in the JUC package that implements this interface, ReentrantReadWriteLock, because it applies to most standard usage contexts. But programmers can create their own implementations for non-standard requirements.

1.2.3 AbstractOwnableSynchronizer/AbstractQueuedSynchronizer/AbstractQueuedLongSynchronizer

    AbstractQueuedSynchronizer is a class called AQS , which is a very useful superclass for defining locks and other synchronizers that rely on queued blocking threads; ReentrantLock, ReentrantReadWriteLock, CountDownLatch, CyclicBarrier and Semaphore are all based on AQS class implemented. The AbstractQueuedLongSynchronizer class provides the same functionality but extends 64-bit support for synchronized state. Both extend the class AbstractOwnableSynchronizer (a simple class that helps keep track of the thread currently maintaining exclusive synchronization).

1.4 LockSupport

        LockSupport provides "creation of locks" and "basic thread blocking primitives for other synchronization classes".

  The function of LockSupport is similar to "Thread.suspend() and Thread.resume() in Thread". The functions of park() and unpark() in LockSupport are to block and unblock threads respectively. But park() and unpark() do not encounter the "deadlock that may be caused by Thread.suspend and Thread.resume".

1.5 Condition

Condition needs to be used in conjunction with Lock. Its role is to replace the Object monitor method. You can sleep/wake up the thread through await() and signal().

The Condition interface describes condition variables that may be associated with locks. These variables are similar in usage to the implicit monitors accessed using Object.wait, but provide more powerful functionality. It is important to point out that a single Lock may be associated with multiple Condition objects. To avoid compatibility issues, the names of the Condition methods are different from those in the corresponding Object version.

1.6 ReentrantLock

    ReentrantLock is an exclusive lock. The so-called exclusive lock refers to a lock that can only be occupied alone, that is, a lock that can only be acquired by one thread lock at the same time point. ReentrantLock locks include "fair ReentrantLock" and "unfair ReentrantLock". "Fair ReentrantLock" means "the mechanism for different threads to acquire locks is fair", while "unfair ReentrantLock" means "the mechanism for different threads to acquire locks is unfair", and ReentrantLock is "reentrant lock"

(01) ReentrantLock implements the Lock interface.
(02) There is a member variable sync in ReentrantLock, sync is of type Sync; Sync is an abstract class, and it inherits from AQS.

(03) ReentrantLock中有"公平锁类"FairSync和"非公平锁类"NonfairSync,它们都是Sync的子类。ReentrantReadWriteLock中sync对象,是FairSync与NonfairSync中的一种,这也意味着ReentrantLock是"公平锁"或"非公平锁"中的一种,ReentrantLock默认是非公平锁。

1.7ReentrantReadWriteLock

    ReentrantReadWriteLock是读写锁接口ReadWriteLock的实现类,它包括子类ReadLock和WriteLock。ReentrantLock是共享锁,而WriteLock是独占锁。

  (01) ReentrantReadWriteLock实现了ReadWriteLock接口。
  (02) ReentrantReadWriteLock中包含sync对象,读锁readerLock和写锁writerLock。读锁ReadLock和写锁WriteLock都实现了Lock接口。

  (03) 和"ReentrantLock"一样,sync是Sync类型;而且,Sync也是一个继承于AQS的抽象类。Sync也包括"公平锁"FairSync和"非公平锁"NonfairSync。

1.8CountDownLatch

CountDownLatch是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。

CountDownLatch包含了sync对象,sync是Sync类型。CountDownLatch的Sync是实例类,它继承于AQS。

1.9CyclicBarrier

CyclicBarrier是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。

       CyclicBarrier是包含了"ReentrantLock对象lock"和"Condition对象trip",它是通过独占锁实现的。
  CyclicBarrier和CountDownLatch的区别是:
  (01) CountDownLatch的作用是允许1或N个线程等待其他线程完成执行;而CyclicBarrier则是允许N个线程相互等待。

  (02) CountDownLatch的计数器无法被重置;CyclicBarrier的计数器可以被重置后使用,因此它被称为是循环的barrier。

2.0Semaphore

Semaphore是一个计数信号量,它的本质是一个"共享锁"。
  信号量维护了一个信号量许可集。线程可以通过调用acquire()来获取信号量的许可;当信号量中有可用的许可时,线程能获取该许可;否则线程必须等待,直到有可用的许可为止。 线程可以通过release()来释放它所持有的信号量许可。

Guess you like

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