好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

业务场景:

一个用户数据接口,要求在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并行场景内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! 

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

查看更多关于Java实现限定时间CountDownLatch并行场景的详细内容...

  阅读:19次