Java实现限定时间CountDownLatch并行场景

2025-05-29 0 72

业务场景:

一个用户数据接口,要求在20ms内返回数据,它的调用逻辑复杂,关联接口多,需要从3个接口汇总数据,这些汇总接口最小耗时也需要16ms,全部汇总接口最优状态耗时需要16ms*3=48ms

解决方案:

使用并行调用接口,通过多线程同时获取结果集,最后进行结果整合。在这种场景下,使用concurrent包的CountDownLatch完成相关操作。CountDownLatch本质上是一个计数器,把它初始化为与执行任务相同的数量,当一个任务执行完时,就将计数器的值减1,直到计算器达到0时,表示完成了所有任务,在await上等待线程就继续执行。

为上述业务场景封装的工具类,传入两个参数:一个参数是计算的task数量,另外一个参数是整个大任务超时的毫秒数。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35
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是一个模拟远程服务的超时的测试类,程序运行后,会输出执行结果到map集合。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
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);

};

}

并行执行获取结果测试类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
@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()执行时间大于20ms,则该方法直接被终止,结果map集没有method1()的结果,结果如下:

总结

使用这种方式,接口能在固定时间内返回,注意CountDownLatch定义数量是任务个数,使用concurrentHashMap避免了并行执行时发生错乱,造成错误的结果的问题。

到此这篇关于Java实现限定时间CountDownLatch并行场景的文章就介绍到这了,更多相关Java CountDownLatch并行场景内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://juejin.cn/post/6979398787651338270

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java实现限定时间CountDownLatch并行场景 https://www.kuaiidc.com/106289.html

相关文章

发表评论
暂无评论