软件编程
位置:首页>> 软件编程>> java编程>> java通过Callable和Future来接收线程池的执行结果

java通过Callable和Future来接收线程池的执行结果

作者:逍遥侯爵  发布时间:2022-03-31 05:01:43 

标签:java,Callable,Future,线程池

在Java的线程执行中,不管是直接继承Thread的方式,还是实现Runnable接口的方式,都不会获取到线程执行的返回结果。这样如果线程在执行过程中出现了错误,那么主线程也不会感知到。即使打印了日志,也不能立即抛出异常。事后查看日志才能发现出现了bug。而且到那时发生问题的代码点距离真正的问题点可能会相差很远。如果在线程池执行的过程中出现了bug能及时地抛出异常,那么这将会是一个很好的实现。解决上述问题的办法是使用Callable接口,其可以获取到线程的返回结果,通过Future的get方法来承接。以下通过一个1000个线程实现累加的例子,来演示Callable和Future的使用:


package com.hys.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

public class Test {

private static AtomicInteger num = new AtomicInteger();

public static void main(String[] args) throws InterruptedException, ExecutionException {
   CountDownLatch latch = new CountDownLatch(1000);
   ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("increment-pool-%d").build();
   ExecutorService poolexecutor = new ThreadPoolExecutor(1000, 1000, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
   Future<String> submit = null;
   for (int i = 0; i < 1000; i++) {
     if (submit != null && submit.get() != null) {
       latch.countDown();
       continue;
     }
     submit = poolexecutor.submit(() -> {
       try {
         //这里模拟一个耗时很长的操作
         num.getAndIncrement();
         //int a = 1 / 0;
         Thread.sleep(1);
         return null;
       } catch (Exception e) {
         return e.toString();
       } finally {
         latch.countDown();
       }
     });
   }
   poolexecutor.shutdown();
   //主线程等待所有分线程执行完毕后再执行
   latch.await();
   String errorMsg = submit.get();
   //如果子线程在执行过程中有错误,则在此抛出该异常
   if (errorMsg != null) {
     throw new RuntimeException(errorMsg);
   }
   System.out.println(num);
 }
}

如果每个线程在执行的过程中没出现问题,则返回的结果为null。如果返回结果不为null,则代表该线程执行的代码有问题,此时将错误信息返回。放开上述第33行代码的注释,以此来模拟一个算术异常,再次执行上述代码,可以得到如下的结果:

Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
 at com.hys.test.Test.main(Test.java:49)

由上可以看到,在主线程抛出了算术异常,可以被感知到。

但是需要注意的一点的是,如果线程的执行结果互相依赖的话,也就是各线程都会调用Future的get方法的话,get方法不得不等待任务执行完成,换言之,如果多个任务提交后,返回的多个Future逐一调用get方法时,将会依次阻塞,任务的执行从并行变为串行。如果想解决该问题,可以考虑使用Java 8中的CompletableFuture来实现。

来源:https://blog.csdn.net/weixin_30342639/article/details/91359503

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com