欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)限定時間CountDownLatch并行場景

 更新時間:2021年07月01日 10:24:28   作者:Gxin  
本文將結(jié)合實例代碼,介紹Java實現(xiàn)限定時間CountDownLatch并行場景,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧

業(yè)務場景:

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

解決方案:

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

為上述業(yè)務場景封裝的工具類,傳入兩個參數(shù):一個參數(shù)是計算的task數(shù)量,另外一個參數(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();
    }
}

當任務運行時間超過了任務的時間上限,就被直接停止,這就是await()的功能。

interface是一個模擬遠程服務的超時的測試類,程序運行后,會輸出執(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é)果測試類

@SpringBootTest
class 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();
    }
}

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

總結(jié)

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

到此這篇關于Java實現(xiàn)限定時間CountDownLatch并行場景的文章就介紹到這了,更多相關Java CountDownLatch并行場景內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家! 

相關文章

最新評論