AQS:AbstractQueuedSynchronizer
1 使用Node實(shí)現(xiàn)FIFO隊(duì)列,可以用于構(gòu)建鎖或者其他同步裝置的基礎(chǔ)框架
2 利用了int類(lèi)型表示狀態(tài)
3 使用方法是繼承
4 子類(lèi)通過(guò)繼承并通過(guò)實(shí)現(xiàn)它的方法管理器狀態(tài){acquire和release}的方法操縱狀態(tài)
5 可以同時(shí)實(shí)現(xiàn)排它鎖和共享鎖模式(獨(dú)占碑隆、共享)
AQS同步組件
1 CountDownLatch:閉鎖,通過(guò)計(jì)數(shù)來(lái)保證線程是否需要一直阻塞
2 Semaphore:控制同一時(shí)間并發(fā)線程的數(shù)目
3 CyclicBarrier:和CountDownLatch相似揭绑,都能阻阻塞線程
4 ReentrantLock
5 Condition
6 FutureTask
CountDownLatch
CountDownLatch是一個(gè)同步輔助類(lèi),應(yīng)用場(chǎng)景:并行運(yùn)算郎哭,所有線程執(zhí)行完畢才可執(zhí)行
package com.ubtechinc.cruzr.lib;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Copyright (C) 2020, UBTECH Robotics
* Description
*
* @author jianlin.duan
* 2020/7/31, Create file
*/
public class CountDownLatchExample {
private final static int TREAD_COUNT = 10;
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(TREAD_COUNT);
for (int i=0;i<TREAD_COUNT;i++) {
final int latchNum = i;
executorService.execute(()->{
try {
Thread.sleep(100);
System.out.println("latchNum="+latchNum);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
countDownLatch.countDown();
}
});
}
//此處一直等到子線程的10次countDown都執(zhí)行完了,才會(huì)被喚醒繼續(xù)
countDownLatch.await();
System.out.println("finish");
}
}
latchNum=8
latchNum=0
latchNum=5
latchNum=4
latchNum=2
latchNum=9
latchNum=3
latchNum=7
latchNum=6
latchNum=1
finish
Semaphore
Semaphore可以很容易控制某個(gè)資源可悲同時(shí)訪問(wèn)的線程個(gè)數(shù)洗做,和CountDownLatch使用有些類(lèi)似,提供acquire和release兩個(gè)方法彰居,acquire是獲取一個(gè)許可诚纸,如果沒(méi)有就等待,release是在操作完成后釋放許可出來(lái)陈惰。Semaphore維護(hù)了當(dāng)前訪問(wèn)的線程的個(gè)數(shù)畦徘,提供同步機(jī)制來(lái)控制同時(shí)訪問(wèn)的個(gè)數(shù),Semaphore可以實(shí)現(xiàn)有限大小的鏈表抬闯,重入鎖(如ReentrantLock)也可以實(shí)現(xiàn)這個(gè)功能井辆,但是實(shí)現(xiàn)上比較復(fù)雜。
Semaphore使用場(chǎng)景:適用于僅能提供有限資源溶握,如數(shù)據(jù)庫(kù)連接數(shù)
package com.ubtechinc.cruzr.lib;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* Copyright (C) 2020, UBTECH Robotics
* Description
*
* @author jianlin.duan
* 2020/7/31, Create file
*/
public class SemaphoreExample {
private final static int TREAD_COUNT = 20;
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
Semaphore semaphore = new Semaphore(3);
for (int i=0;i<TREAD_COUNT;i++) {
final int latchNum = i;
executorService.execute(()->{
try {
//每5s內(nèi)只能執(zhí)行3個(gè)子線程
semaphore.acquire();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd mm:ss");
System.out.println(sdf.format(new Date())+" latchNum="+latchNum);
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();
}
});
}
}
}
第一個(gè)5s
2020-07-31 34:37 latchNum=1
2020-07-31 34:37 latchNum=2
2020-07-31 34:37 latchNum=0
第二個(gè)5s
2020-07-31 34:42 latchNum=5
2020-07-31 34:42 latchNum=3
2020-07-31 34:42 latchNum=4
...
2020-07-31 34:47 latchNum=8
2020-07-31 34:47 latchNum=6
2020-07-31 34:47 latchNum=7
2020-07-31 34:52 latchNum=9
2020-07-31 34:52 latchNum=12
2020-07-31 34:52 latchNum=10
2020-07-31 34:57 latchNum=13
2020-07-31 34:57 latchNum=11
2020-07-31 34:57 latchNum=14
2020-07-31 35:02 latchNum=16
2020-07-31 35:02 latchNum=17
2020-07-31 35:02 latchNum=15
2020-07-31 35:07 latchNum=18
2020-07-31 35:07 latchNum=19
CyclicBarrier
與CountDownLatch相似杯缺,都是通過(guò)計(jì)數(shù)器實(shí)現(xiàn),當(dāng)某個(gè)線程調(diào)用await方法睡榆,該線程就進(jìn)入等待狀態(tài)萍肆,且計(jì)數(shù)器進(jìn)行加1操作,當(dāng)計(jì)數(shù)器的值達(dá)到設(shè)置的初始值胀屿,進(jìn)入await等待的線程會(huì)被喚醒塘揣,繼續(xù)執(zhí)行他們后續(xù)的操作。由于CyclicBarrier在釋放等待線程后可以重用宿崭,所以又稱循環(huán)屏障亲铡。使用場(chǎng)景和CountDownLatch相似,可用于并發(fā)運(yùn)算葡兑。
CyclicBarrier和CountDownLatch區(qū)別:
1 CountDownLatch計(jì)數(shù)器只能使用一次奖蔓,CyclicBarrier的計(jì)數(shù)器可以使用reset方法重置循環(huán)使用
2 CountDownLatch主要是視線1個(gè)或n個(gè)線程需要等待其他線程完成某項(xiàng)操作才能繼續(xù)往下執(zhí)行,CyclicBarrier主要是實(shí)現(xiàn)多個(gè)線程之間相互等待知道所有線程都滿足了條件之后才能繼續(xù)執(zhí)行后續(xù)的操作讹堤,CyclicBarrier能處理更復(fù)雜的場(chǎng)景
package com.ubtechinc.cruzr.lib;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* Copyright (C) 2020, UBTECH Robotics
* Description
*
* @author jianlin.duan
* 2020/7/31, Create file
*/
public class CyclicBarrierExample {
private final static int TREAD_COUNT = 20;
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
ExecutorService executorService = Executors.newCachedThreadPool();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd mm:ss");
CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
for (int i=0;i<9;i++) {
final int latchNum = i;
executorService.execute(()->{
try {
//等10個(gè)線程都跑到此處,才喚醒繼續(xù)執(zhí)行
System.out.println(sdf.format(new Date())+" latchNum="+latchNum);
cyclicBarrier.await();
System.out.println(sdf.format(new Date())+" latchNum="+latchNum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
});
}
Thread.sleep(5000);
cyclicBarrier.await();
System.out.println(sdf.format(new Date())+" finish");
}
}
2020-07-31 39:21 latchNum=1
2020-07-31 39:21 latchNum=4
2020-07-31 39:21 latchNum=8
2020-07-31 39:21 latchNum=7
2020-07-31 39:21 latchNum=2
2020-07-31 39:21 latchNum=6
2020-07-31 39:21 latchNum=3
2020-07-31 39:21 latchNum=5
2020-07-31 39:21 latchNum=0
前9個(gè)線程執(zhí)行到等待位置
2020-07-31 39:26 finish
第十個(gè)線程也執(zhí)行到等待位置,且執(zhí)行完畢,此時(shí)10條線程一起繼續(xù)執(zhí)行
2020-07-31 39:26 latchNum=1
2020-07-31 39:26 latchNum=8
2020-07-31 39:26 latchNum=7
2020-07-31 39:26 latchNum=6
2020-07-31 39:26 latchNum=2
2020-07-31 39:26 latchNum=4
2020-07-31 39:26 latchNum=0
2020-07-31 39:26 latchNum=5
2020-07-31 39:26 latchNum=3
Condition
多線程建協(xié)調(diào)通信的工具類(lèi)
package com.ubtechinc.cruzr.lib;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* Copyright (C) 2020, UBTECH Robotics
* Description
*
* @author jianlin.duan
* 2020/7/31, Create file
*/
public class ConditionExample {
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd mm:ss");
ReentrantLock reentrantLock = new ReentrantLock();
Condition condition = reentrantLock.newCondition();
new Thread(()->{
reentrantLock.lock();
try {
condition.await();
System.out.println(sdf.format(new Date())+" recevied signal");
} catch (InterruptedException e) {
e.printStackTrace();
}
reentrantLock.unlock();
}).start();
new Thread(()->{
reentrantLock.lock();
try {
System.out.println(sdf.format(new Date())+" getlock");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sdf.format(new Date())+" send signal");
condition.signalAll();
reentrantLock.unlock();
}).start();
}
}
2020-07-31 55:16 await signal
2020-07-31 55:16 getlock
2020-07-31 55:19 send signal
2020-07-31 55:19 recevied signal
本文參考https://blog.csdn.net/GavinZhera/article/details/86471828