page contents

在一个接口中串行调用3个本类中的其他方法修改成并行出现空指针问题,help

Pack 发布于 2020-02-09 17:37
阅读 419
收藏 0
分类:Java开发

问题,在一个接口中串行调用3个本类中的其他方法是可以返回数据的,代码如下:

   DubboResult<TodaySuperviseDataResDTO> todays = getTodaySuperviseData(todaySuperviseDataReqDTO);
    DubboResult<DelayedWorkOrderStatisticResDTO> delays =getDelayedWorkOrderStatistic(delayedWorkOrderStatisticReqDTO);
    DubboResult<WorkOderSuperviseStatisticResDTO> orders = getWorkOderSuperviseStatistic(workOderSuperviseStatisticReqDTO);

修改成并行调用get()方法时出现空指针:

ExecutorService executorService = Executors.newFixedThreadPool(3);
CountDownLatch countDownLatch = new CountDownLatch(3);Future<DubboResult<TodaySuperviseDataResDTO> > todays = executorService.submit(() -> {  try {    return getTodaySuperviseData(todaySuperviseDataReqDTO);  } finally {    countDownLatch.countDown();  }});Future<DubboResult<DelayedWorkOrderStatisticResDTO>> delays = executorService.submit(() -> {  try {    return getDelayedWorkOrderStatistic(delayedWorkOrderStatisticReqDTO);  } finally {    countDownLatch.countDown();  }});Future<DubboResult<WorkOderSuperviseStatisticResDTO>> orders = executorService.submit(() -> {  try {    return  getWorkOderSuperviseStatistic(workOderSuperviseStatisticReqDTO);  } finally {    countDownLatch.countDown();  }});System.out.println(todays.get().getData());System.out.println(delays.get().getData());System.out.println(orders.get().getData());

这是什么原因造成的呢?麻烦各位泡友们解答一下小弟的疑惑。

295
Pack
Pack

你可以用debug走一下,估计是你多线程启动之后,多线程还有执行结束,就调用下面system.out方法导致的

请先 登录 后评论