您的位置:首页 > 教程文章 > 编程开发

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

:0 :2021-10-27 22:02:18

目录
业务场景:
解决方案:
总结
业务场景:
一个用户数据接口,要求在20ms内返回数据,它的调用逻辑复杂,关联接口多,需要从3个接口汇总数据,这些汇总接口最小耗时也需要16ms,全部汇总接口最优状态耗时需要16ms*3=48ms
解决方案:
使用并行调用接口,通过多线程同时获取结果集,最后进行结果整合。在这种场景下,使用concurrent包的CountDownLatch完成相关操作。CountDownLatch本质上是一个计数器,把它初始化为与执行任务相同的数量,当一个任务执行完时,就将计数器的值减1,直到计算器达到0时,表示完成了所有任务,在await上等待线程就继续执行。
为上述业务场景封装的工具类,传入两个参数:一个参数是计算的task数量,另外一个参数是整个大任务超时的毫秒数。
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集合。
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);
    };
}
并行执行获取结果测试类
@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并行场景内容请搜索无名以前的文章或继续浏览下面的相关文章希望大家以后多多支持无名!

java使用GeoTools读取shp文件并画图的操作代码
MultipartFile中transferTo(File file)的路径问题及解决

同类资源