「AQS」是AbstractQueuedSynchronizer的简称,它提供了一种等待唤醒的机制。底层核数据结构是双端队列。
采用自旋 + LockSupport + CAS来实现这种等待唤醒。在java的「JUC」并发包下很多类都是基于「AQS」实现的。下面我们来介绍几种并发类。
俗称「信号量」,用于控制在同一时间内共享资源被多少线程共享。可以作为流浪控制。
如上代码,我用Semaphore来控制semaphoreDemo.service(vipNo)的请求数量。如果并发时间请求数量超过5,弹出"获取令牌失败..."。
俗称「栅栏」机制,用于控制线程在同一时间点并发请求。
英语好的可以看看官方解释
❝A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point . The barrier is called cyclic because it can be re-used after the waiting threads are released.
❞当线程数满20时,才会并发执行orderService.createOrder()。
俗称倒计数器,允许一个或多个线程一直等待,直到一组在其他线程执行的操作全部完成。当一个线程调用await方法时,就会阻塞当前线程。每当有线程调用一次 countDown 方法时,计数就会减 1。当 count 的值等于 0 的时候,被阻塞的线程才会继续运行。
源码解释如下
❝A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon – the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.
A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown(). A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.
❞如上代码,可用于当收到服务器响应,才返回zookeeper。
Semaphore比较好理解,需要拿到许可才能执行,并可以选择公平和非公平模式。
对于CyclicBarrier和CountDownLatch我总结一下几点:
❝CountDownLatch 是一个线程等待其他线程,CyclicBarrier是多个线程互相等待。
CountDownLatch 是一次性的, CyclicBarrier 可以循环利用。
❞这篇文章只是初步介绍「AQS」并发类的一些使用场景,后续我会讲解它们的底层原理,敬请期待。
我写出这样干净的代码,老板直夸我
云南丽江旅游攻略
使用ThreadLocal怕内存泄漏?
Java进阶之路思维导图
程序员必看书籍推荐
3万字的Java后端面试总结(附PDF)
扫码二维码,获取更多精彩。或微信搜Lvshen_9,可后台回复获取资料
1.回复"java" 获取java电子书; 2.回复"python"获取python电子书; 3.回复"算法"获取算法电子书; 4.回复"大数据"获取大数据电子书; 5.回复"spring"获取SpringBoot的学习视频。 6.回复"面试"获取一线大厂面试资料 7.回复"进阶之路"获取Java进阶之路的思维导图 8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版) 9.回复"总结"获取Java后端面试经验总结PDF版 10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF) 11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)另:点击【我的福利】有更多惊喜哦。