zoukankan      html  css  js  c++  java
  • future封装了callable,thread封装future。

    三、使用Callable,Future返回结果

    总结:future封装了callable,thread封装future。将callable的返回结果封装在future中,thread封装future,这样thread执行完后,就可以从future中拿取线程执行结果。

    总结:future封装了callable,thread封装future。将callable的返回结果封装在future中,thread封装future,这样thread执行完后,就可以从future中拿取线程执行结果。

    总结:future封装了callable,thread封装future。将callable的返回结果封装在future中,thread封装future,这样thread执行完后,就可以从future中拿取线程执行结果。

    Future<V>代表一个异步执行的操作,通过get()方法可以获得操作的结果,如果异步操作还没有完成,则,get()会使当前线程阻塞。FutureTask<V>实现了Future<V>和Runable<V>。Callable代表一个有返回值得操作。

    Java代码  收藏代码
    1. Callable<Integer> func = new Callable<Integer>(){  
    2.     public Integer call() throws Exception {  
    3.         System.out.println("inside callable");  
    4.         Thread.sleep(1000);  
    5.         return new Integer(8);  
    6.     }         
    7. };        
    8. FutureTask<Integer> futureTask  = new FutureTask<Integer>(func);  
    9. Thread newThread = new Thread(futureTask);  
    10. newThread.start();  
    11.   
    12. try {  
    13.     System.out.println("blocking here");  
    14.     Integer result = futureTask.get();  
    15.     System.out.println(result);  
    16. catch (InterruptedException ignored) {  
    17. catch (ExecutionException ignored) {  
    18. }  

     ExecutoreService提供了submit()方法,传递一个Callable,或Runnable,返回Future。如果Executor后台线程池还没有完成Callable的计算,这调用返回Future对象的get()方法,会阻塞直到计算完成。

  • 相关阅读:
    Excel宏打开URL 特殊字符#变了
    windows升级到1607后操作很卡顿的解决办法
    打包解决方案后,安装时提示只能在IIS5.1以上运行解决方法
    在CS代码页获取input输入框内肉----.net学习点滴
    greenplum 6.7编译安装插件pxf
    greenplum 6.7编译安装插件mysql_fdw
    python3 + greenplum-spark-connector访问greenplum
    centos7无cm安装hadoop+spark
    hive实现update和delete功能
    使得hivecli访问hive
  • 原文地址:https://www.cnblogs.com/panxuejun/p/10198044.html
Copyright © 2011-2022 走看看