久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java實(shí)現(xiàn)限定時(shí)間CountDownLatch并行場(chǎng)景

瀏覽:4日期:2022-08-10 16:07:42
目錄業(yè)務(wù)場(chǎng)景:解決方案:總結(jié)業(yè)務(wù)場(chǎng)景:

一個(gè)用戶數(shù)據(jù)接口,要求在20ms內(nèi)返回?cái)?shù)據(jù),它的調(diào)用邏輯復(fù)雜,關(guān)聯(lián)接口多,需要從3個(gè)接口匯總數(shù)據(jù),這些匯總接口最小耗時(shí)也需要16ms,全部匯總接口最優(yōu)狀態(tài)耗時(shí)需要16ms*3=48ms

解決方案:

使用并行調(diào)用接口,通過多線程同時(shí)獲取結(jié)果集,最后進(jìn)行結(jié)果整合。在這種場(chǎng)景下,使用concurrent包的CountDownLatch完成相關(guān)操作。CountDownLatch本質(zhì)上是一個(gè)計(jì)數(shù)器,把它初始化為與執(zhí)行任務(wù)相同的數(shù)量,當(dāng)一個(gè)任務(wù)執(zhí)行完時(shí),就將計(jì)數(shù)器的值減1,直到計(jì)算器達(dá)到0時(shí),表示完成了所有任務(wù),在await上等待線程就繼續(xù)執(zhí)行。

為上述業(yè)務(wù)場(chǎng)景封裝的工具類,傳入兩個(gè)參數(shù):一個(gè)參數(shù)是計(jì)算的task數(shù)量,另外一個(gè)參數(shù)是整個(gè)大任務(wù)超時(shí)的毫秒數(shù)。

import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class ParallelCollector { private Long timeout; private CountDownLatch countDownLatch; ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 200, 1, TimeUnit.HOURS, new ArrayBlockingQueue<>(100)); public ParallelCollector(int taskSize, Long timeOutMill) {countDownLatch = new CountDownLatch(taskSize);timeout = timeOutMill; } public void submitTask(Runnable runnable) {executor.execute(() -> { runnable.run(); countDownLatch.countDown();}); } public void await() {try { this.countDownLatch.await(timeout, TimeUnit.MILLISECONDS);} catch (InterruptedException e) { e.printStackTrace();} } public void destroy() {this.executor.shutdown(); }}

當(dāng)任務(wù)運(yùn)行時(shí)間超過了任務(wù)的時(shí)間上限,就被直接停止,這就是await()的功能。

interface是一個(gè)模擬遠(yuǎn)程服務(wù)的超時(shí)的測(cè)試類,程序運(yùn)行后,會(huì)輸出執(zhí)行結(jié)果到map集合。

public class InterfaceMock { private volatile int num=1; public String slowMethod1() {try { Thread.sleep(2000);} catch (InterruptedException e) { e.printStackTrace();}return String.valueOf(num+1); }; public String slowMethod2() {return String.valueOf(num+1); }; public String slowMethod3() {return String.valueOf(num+1); };}

并行執(zhí)行獲取結(jié)果測(cè)試類

@SpringBootTestclass ThreadPoolApplicationTests { @Test void testTask() {InterfaceMock interfaceMock = new InterfaceMock();ParallelCollector collector = new ParallelCollector(3, 20L);ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();collector.submitTask(()->map.put('method1',interfaceMock.slowMethod1()));collector.submitTask(()->map.put('method2',interfaceMock.slowMethod2()));collector.submitTask(()->map.put('method3',interfaceMock.slowMethod3()));collector.await();System.out.println(map.toString());collector.destroy(); }}

當(dāng)method1()執(zhí)行時(shí)間大于20ms,則該方法直接被終止,結(jié)果map集沒有method1()的結(jié)果,結(jié)果如下:

總結(jié)

使用這種方式,接口能在固定時(shí)間內(nèi)返回,注意CountDownLatch定義數(shù)量是任務(wù)個(gè)數(shù),使用concurrentHashMap避免了并行執(zhí)行時(shí)發(fā)生錯(cuò)亂,造成錯(cuò)誤的結(jié)果的問題。

到此這篇關(guān)于Java實(shí)現(xiàn)限定時(shí)間CountDownLatch并行場(chǎng)景的文章就介紹到這了,更多相關(guān)Java CountDownLatch并行場(chǎng)景內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 莱阳市| 嵊泗县| 北安市| 铅山县| 且末县| 万安县| 苏州市| 南岸区| 二连浩特市| 福建省| 临猗县| 襄垣县| 淳化县| 榆林市| 自治县| 谷城县| 揭东县| 明光市| 阜南县| 玉溪市| 自治县| 石柱| 左贡县| 湖北省| 康保县| 江北区| 福州市| 临沭县| 军事| 松阳县| 盈江县| 高平市| 福安市| 云梦县| 电白县| 虞城县| 光山县| 海阳市| 巴林右旗| 阿拉善左旗| 上栗县|