Overview of JUC concurrency classes and concurrency related classes, continuing to supplement…

  • AQS

There are two internal queues, one waiting queue (front and back nodes) and one conditional queue (successor nodes), which is actually implemented by linked list; The wait queue is a bidirectional linked list; Conditional queues are unidirectional linked lists; If the conditional queue is awakened, it will be connected to the waiting queue. Through internally held state and template mode, two modes of resource competition are provided: exclusive and shared; And in the scramble, with the consideration of fair and unfair competition; Boolean tryAcquire and (tryAcquireShared(x) >= 0. State can only be changed by one thread (CAS(0,1)). Sharing, on the other hand, allows multiple threads to change the state value simultaneously (to be fetched by multiple threads). The method name is -interruptible. If it is concerned, the exception is thrown. If it is not concerned, the thread’s interrupt flag is cleared and the thread continues to suspend until successful. In summary, the branching considerations are: resource type (whether exclusive) -> whether fair -> whether interrupts are concerned (each in two directions); When releasing resources, the exclusive type needs to determine whether the current thread exists, while the shared type does not need to determine the thread and only cares about the resource pool. The exclusive type wakes up the next node in the waiting queue, while the share is signaled in a cascading manner, starting at the head and continuing until the resource is snapped up by the newly awakened thread. Threads are suspended through locksupport. park(thread), which supports suspension for a limited time. The state release causes the wait queue to decide, and thus wake up. A timed suspend must support an interrupt response; Queue for initial condition is through the new ConditionObject (), you can create many times, so the queue to support multiple conditions, using the same object is the same condition the queue; State resource acquisition will be performed when wakened, and waiting queue will be entered if not. Also supports whether to interrupt attention; The interrupt of conditional queue has two cases, one is awakened by interrupt on conditional queue, the other is awakened by interrupt after entering waiting queue; AQS is mainly the control of CAS concurrency and thread interruption, suspension and queuing of resource state

  • AQLS

State is of type long and extends the number of concurrent requests (future extensions). Others are the same as AQS. Long needs to be concerned about cache rows.

  • ReentrantLock

Lock based on AQS, set state = 1; Support fair and unfair (determine whether the queue has waiting threads); Support reentrant, state increment; In the competition, the lock is successfully acquired only when state is 0; otherwise, the lock enters the waiting queue. Support await and signal for condition.

  • CopyOnWriteArrayList

Unrelated to AQS, synchronized is used to ensure the safety of synchronization; When an add occurs, a copy of the array is added and the reference is updated. When the specified index is added, it locates the index and copies it in two steps (before and after the index). Suitable for reading more and writing less scene; AddIfAbsent determines whether (no synchronization lock is available) and then copies (with synchronization lock) a snapshot. If yes, the snapshot is used. If no, copy the snapshot again.

  • CopyOnWriteArraySet

The bottom layer uses CopyOnWriteArrayList. The unique addition uses addIfAbsent. Most of the other methods call CopyOnWriteArrayList’s similar methods directly;

  • CountDownLatch

With AQS, initialize to state, countDown to state-1 and await to getState() == 0? 1: -1 is not in the queue by suspending, so how many states will it wake up state times and then suspend again until state = 0;

  • ArrayBlockingQueue

Instead of using or inheriting AQS directly, we use ReentrantLock and Condition to perform multi-threaded synchronization and suspension. Internal array is used to store elements, but will not be extended, the realization of ring queue storage, queue element increase offer and poll are synchronized with lock; Notempty.signal () notifies the thread (notempty.await ()) that has failed when adding successfully; On success, a notfull.signal () notification is made to add the failed thread (notfulll.await ()). Offer (E) add returns true/false; Add (E) calls offer internally, returns false and throws an exception; Put is self-implemented. If it cannot be added, block. Poll () returns E/null, take is implemented, if not, block and suspend; NotEmpty and notFull are notified on success of any add or get, and the opposite queue is suspended on failure. Concurrent traversal of ArrayBlockingQueue is implemented by queue extension (i.e., one queue, rolling over and over); It’s like a ring queue