当前位置:网站首页>AQS-AbstractQueuedSynchronizer

AQS-AbstractQueuedSynchronizer

2022-08-02 11:31:00 Is the food dish XiaoYan pity ah

目录

AQS简介

AQS具备的特性

AQS内部维护属性

AQS资源共享方式

AQS队列

AQS队列中节点状态

 AQSImplemented thread wait queue maintenance method

同步等待队列

 条件等待队列

Condition


AQS简介

Previous article finishing,知道了synchronized的使用,以及synchronized的原理,还有管程

The monitor is to ensure concurrency safety by locking,用的比较多的,就是MESA

MESAInternally there are synchronous wait queues and many conditional wait queues,Threads that fail to acquire the lock will enter the synchronization wait queue

而synchronized是JVM层面对管程的实现,是基于对象的,通过ObjectMonitor来实现,获取锁失败的线程,会进入到cxq队列,This is the synchronous wait queue,而wait,notify,notifyAll,It is a conditional waiting queue 

但是synchronized也有缺点:自动加锁解锁,Manual unlocking is not supported;Implementation is heavier

这是synchronized的缺点,And there is no way to change it

那,在保证性能的前提下,JavaHow to implement the monitoring process at the level,So the big developer came up with the next thing

并发这块,Used a lot of locks,除了synchronized,很多人都会想到ReentrantLock,而ReentrantLock也是有基础的,那就是基于AQS

AQS是什么,就是AbstractQueuedSynchronizer的简写

java.util.concurrentMost synchronizer implementations in the package,如Lock、Latch、Barrier,All revolve around a common basic behavior,比如等待队列、条件队列、独占获取、shared resources, etc,The abstraction of these behaviors is based onAQS实现的,AQS是一个抽象同步框架,可以用来实现一个依赖状态的同步器

AQS具备的特性

AQS具备如下特性:

  • 阻塞等待队列
  • 共享/独占
  • 公平/非公平
  • 可重入
  • 允许中断

AQS内部维护属性

volatile int state

state表示资源的可用状态,访问state,有三种方式

getState(), setState(), compareAndSetState()

AQS资源共享方式

AQS定义了两种资源共享方式

  • Exclusive-独占,只有一个线程能执行,比如,ReentrantLock
  • Share-共享,多个线程可以同时执行,比如,Semaphore、CountDownLatch

AQS队列

AQS有两种队列,They are the synchronous waiting queue and the conditional waiting queue, respectively

The synchronous waiting queue is mainly used to maintain the thread that enters the pair when the acquisition of the mutex fails

Conditional wait queues are mainly used,线程调用await()method to release the lock,进入条件队列,等调用了signal()方法唤醒的时候,Will move the thread node in the condition queue to the synchronization queue,Waiting to reacquire the lock

AQS队列中节点状态

AQSDefines the node state of five threads in the queue

  • 值为0,初始化状态,表示当前节点在sync队列中,等待获取锁
  • CANCELLED,值为1,表示当前线程被取消
  • SIGNAL,值为-1,表示当前节点的后继节点包含的线程需要运行,也就是unpark
  • CONDITION,值为-2,表示当前节点在等待condition,也就是在condition队列中
  • PROPAGATE,值为-3,Indicates the subsequent ones in the current field sceneacquireshared能够得以执行

 AQSImplemented thread wait queue maintenance method

Different custom synchronizers compete for shared resources differently,自定义同步器在实现时只需要实现共享资源stateThe acquisition and release can be done,As for the specific thread waiting for the maintenance of the queue,For example, resource acquisition fails to enter the queue,Wake up to dequeue and so on,AQSAlready implemented at the top level,自定义同步器实现时主要实现以下几种方法

  • isHeldExclusively():该线程是否正在独占资源,只有用到了condition才需要去实现它
  • tryAcquire(int):独占方式,尝试获取资源,成功就返回true,失败就返回false
  • tryRelease(int):独占方式,尝试释放资源,成功就返回true,失败就返回false
  • tryAcquireShared(int):共享方式,尝试获取资源,负数表示失败;0表示成功,但是iThere are no remaining resources available;The letter indicates success,And there are remaining resources
  • tryReleaseShared(int):共享方式,尝试释放资源,如果释放后允许唤醒后续等待节点返回true,否则返回false

同步等待队列

AQSSynchronous wait queue in ,也称为CLH队列,It is a queue based on a doubly linked list structure,It is a first-in, first-out waiting queue,AQS依赖于CLHQueue to complete the management of the synchronization state,至于为什么叫CLH队列,那是因为,CLH队列是Craig、Landin、HagerstenInvented by three people

如果当前线程获取同步状态失败,AQSIt will construct information such as the current thread's waiting state into a node,并将其加入到CLH队列,同时会阻塞当前线程

当同步状态被释放时,will wake up the first node(公平锁),Make it try to get sync status here

通过signal或者signalAllNodes in the conditional queue can be transferred to the synchronization queue

 条件等待队列

AQSMedium condition waiting queues are kept in a one-way list,用nextWaiter来拼接

调用await()方法阻塞线程

当前线程存在于同步队列的头节点,调用await()方法进行阻塞,Thus, the synchronization queue is converted into a conditional queue

Condition

可以将ConditionCommonly understood as a conditional queue

调用Condition#await方法会释放当前持有的锁,然后阻塞当前线程,同时向Condition队列尾部添加一个节点,所以调用Condition#await方法的时候必须持有锁

调用Condition#signal方法会将Condition队列的首节点移动到阻塞队列尾部,然后唤醒因调用Condition#await方法而阻塞的线程(唤醒之后这个线程就可以去竞争锁了),所以调用Condition#signal方法的时候必须持有锁,持有锁的线程唤醒被因调用Condition#await方法而阻塞的线程

原网站

版权声明
本文为[Is the food dish XiaoYan pity ah]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208021127029022.html