多线程拆分时间查询数据库(结果无序)

@Test
    void test22() throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(2);

        String start = "2022-07-01 00:00:00";

        String end = "2022-08-15 00:00:00";

        List<String> list = cutDate("D", start, end);

        ExecutorService threadPool = new ThreadPoolExecutor(
                8, //corePoolSize线程池中核心线程数
                10, //maximumPoolSize 线程池中最大线程数
                60, //线程池中线程的最大空闲时间,超过这个时间空闲线程将被回收
                TimeUnit.SECONDS,//时间单位
                new ArrayBlockingQueue(500), //队列
                new ThreadPoolExecutor.CallerRunsPolicy()); //拒绝策略

        String d1 = list.get(0);
        String d2 = list.get(1);
        String d3 = list.get(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<DrugDoseYzDataDto> result = new ArrayList<>();


        threadPool.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                List<DrugDoseYzDataDto> drugDoseList = yzDataMapper.getDrugDoseList(sdf.parse(d1), sdf.parse(d2));
                result.addAll(drugDoseList);
                countDownLatch.countDown();
            }
        });

        threadPool.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                List<DrugDoseYzDataDto> drugDoseList = yzDataMapper.getDrugDoseList(sdf.parse(d2), sdf.parse(d3));
                result.addAll(drugDoseList);
                countDownLatch.countDown();
            }
        });

        countDownLatch.await();  //等待  (线程结束时间)

        System.out.println(result);

    }

 

posted @ 2022-08-15 17:29  古家杰  阅读(109)  评论(0)    收藏  举报