美文网首页
java线程:Future和Callable

java线程:Future和Callable

作者: cjsssss | 来源:发表于2018-01-07 15:37 被阅读0次

java线程:Future和Callable

前两天在公司的项目看到FutureCallable的使用,有点不理解。
这个Futurecabllable到底是什么东西?我都对这个东西感到特别奇怪就我现在的理解,
因为本人在并发这方面没有多少实践经验,所以只好就着一些资料和自己的理解给它下个定义,
Future就是保存我们任务的完成信息,简单的说就是一个产生结果,一个拿到结果。
Callable有点类似于Runable接口,只是Runable接口不能返回结果,而Callable的功能更加强大点,当
线程执行的时候,它可以产生一个返回值。而这个返回值就可以被Future拿到,所以Future可以拿到异步执行任务的
返回值。
先看一个简单的例子:

public static void main(String[] args) {
     Callable<Integer> callable=new Callable<Integer>(){
       @Override
       public Integer call() throws Exception{
           return new Random().nextInt(100);
       }
     };
     FutureTask<Integer> future=new FutureTask<Integer>(callable);
     new Thread(future).start();
     try {
         System.out.println(future.get());
     } catch (InterruptedException e) {
         e.printStackTrace();
     } catch (ExecutionException e) {
         e.printStackTrace();
     }
 }

FutureTask实现了RunnableFuture,而RunnableFuture两个接口,RunnableFuture,说明FutureTask可以
作为Runable被线程执行,又可以作为Future得到Callable的返回值,所以我们可以想到就是在
当有一组算法执行相对较慢,又想拿到返回值的时候,那么就可以使用这个组合,用另一个线程去计算返回值,
而当前线程在使用这个返回值之前可以做其它的操作,等到需要这个返回值时,再通过Future得到。
下一种使用Future得到Callable的返回值的方法,可以使用ExecutorServicesumbit的方法
执行Callable,并用Future去到返回值

public static void main(String[] args) {
      ExecutorService executorService= Executors.newSingleThreadExecutor();
      Future<Integer> future=executorService.submit(new Callable<Integer>() {
          @Override
          public Integer call() throws Exception {
              return new Random().nextInt(500);
          }
      });
      try {
          System.out.println(future.get());
      } catch (InterruptedException e) {
          e.printStackTrace();
      } catch (ExecutionException e) {
          e.printStackTrace();
      }
  }

ExecutorService继承自Executor,它的目的是为我们管理Thread对象,从而简化并发编程,
Executor使我们无需显示的去管理线程的生命周期。

我们还可可以使用Future的集合,去的Callable返回的多个值

public static void main(String[] args) {
    ExecutorService threadPool= Executors.newCachedThreadPool();
    List<Future<Integer>> futureList=new ArrayList<>();
        for (int i = 0; i <5 ; i++) {
            Future<Integer> future= threadPool.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                        return new Random().nextInt(50);
                }
            });
            futureList.add(future);
        }
        futureList.stream().forEach(s->{
            try {
                System.out.println(s.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }

相关文章

网友评论

      本文标题:java线程:Future和Callable

      本文链接:https://www.haomeiwen.com/subject/zuapnxtx.html