weixin_44040479 2021-08-17 16:10 采纳率: 0%
浏览 171
已结题

线程池怎么将执行的结果全部返回

问题场景:
需要在线程池中的线程执行完后将数据全部返回,但是现在是返回了部分的数据,未将线程池中的线程的执行结果全部返回

     JSONObject res = new JSONObject(); //返回前台的数据
    long start = System.currentTimeMillis(); 
    log.info("======接入系統上報量線程執行開始=======");
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(   // 定义线程池
           352L,
           TimeUnit.SECONDS,
           new ArrayBlockingQueue<>(3),
           Executors.defaultThreadFactory(),
           new ThreadPoolExecutor.AbortPolicy()
    );

  try{
            statisticSystemNum.stream().forEach(t -> {
                      threadPoolExecutor.execute(() ->{
                        t.put("value",  tj07Mapper.countOneByOne(0,druidType,gc0100));// 查询数据库,并将数据设置到res中
                        log.info("執行了tttt====>" + t.get("value"));
            });
        });
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        threadPoolExecutor.shutdown();
        long end = System.currentTimeMillis();
        log.info("======接入系統上報量線程執行結束=====共花費=="+(end-start)+"ms");
    }
     res.put("statisticSystemNum",statisticSystemNum);
      return res;
  }

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2021-08-17 16:22
    关注

    使用ThreadGroup实现主线程等待所有子线程完成。

     
    public class ThreadGroupDemo {
        public static void main(String[] args) {
            System.out.println("main start");
            ThreadGroup tg = new ThreadGroup("Parent ThreadGroup");
            for (int j = 0; j < 10; j++) {
                new Thread(tg, "t" + j) {
                    public void run() {
                        System.out.println("Thread: " + getName() + " running");
                        int small = 0;
                        int large = 10;
                        try {
                            Thread.sleep(1000 * (int) (Math.random() * (large - small + 1) + small));
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
            while (tg.activeCount() > 0) {
                try {
                    System.out.println("Waiting for " + tg.activeCount() + " CThreads to Complete");
                    Thread.sleep(1000); // Main Thread or someThradObject.sleep();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("main end");
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月13日
  • 创建了问题 8月17日

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿