获取线程结果的future.get()返回值类型

本文介绍了如何在Java中创建一个自定义线程`ReportThread`实现`Callable`接口,通过`ExecutorService`进行并发任务调度,以及如何使用`Future`获取异步任务的结果。
摘要由CSDN通过智能技术生成

下面是我写的自定义线程

private class ReportThread implements Callable<List<ReportV>> {

        public String time;

        public String tableCode;

        public String company;

        public String insertTime;

        public Map<String, String> columnName1;

        public Map<String, String> columnName2;

        public List<Map<String, String>> columnMapList;

        public List<ReportV> reportVs;

        @Override
        public List<ReportV> call() throws Exception {
            try {
                if (Objects.isNull(columnName2)) {
                    reportVs= dataController.selectData(tableCode, columnName1, time, company, insertTime);
                } else {
                    reportVs= dataController.selectData(tableCode, columnName1, time, company, insertTime);
                    reportVs.addAll(dataController.selectData(tableCode, columnName2, time, company, insertTime));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return reportVs;
        }
    }

调用自定义线程的代码

   private List<ReportV> executor() throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
//        ExecutorService executorService = Executors.newCachedThreadPool();
        List<Callable<List<ReportV>>> tasks = new ArrayList<>(); 
        //和future的返回值保持一致,才能正常取回

        if (!quarterFlag) {
            ReportThread reportThread1 = new ReportThread();
            reportThread1.setTableCode("xxx");
            tasks.add(reportThread1);
            //executorService.submit(reportThread1);

            ReportThread reportThread2 = new ReportThread();
            reportThread2.setTableCode("ooo");
            tasks.add(reportThread2);
            
        } else {
            ReportThread reportThreadQuarter = new ReportThread();
            reportThreadQuarter.setTableCode("kkk");
            tasks.add(nccReportThreadQuarter);
        }

        List<Future<List<ReportV>>> futures = executorService.invokeAll(tasks);
        executorService.shutdown();//停止接收新任务
//        shutdownNow():停止接收新任务,原来的任务停止执行
        while (!executorService.isTerminated()) {

        }
        List<ReportV> reportVS = new ArrayList<>();
        futures.forEach(item -> {
            try {
                reportVS.addAll(item.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        log.info("###reportVS= {}", reportVS);
        return reportVS;
    }

获取线程的返回值,这里使用的是future.get()这个方法。但是返回值类型需要在自定义线程的实施类后面定义,如下:

ReportThread implements Callable<List<ReportV>//Callable<T>

随后是定义Callable的类型:

List<Callable<List<ReportV>>> tasks = new ArrayList<>(); 
//和future的返回值保持一致,才能正常取回

这样之后调用future.get()的时候才能够取到ReportV这个对象的List。

如有不对,请多指教。

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值