A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xgm 中级黑马   /  2016-3-18 00:37  /  348 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. * 目的:了解callable、future
  2. *
  3. */
  4. public class CallableAndFutureTest {

  5.         public static void main(String[] args) {
  6.                
  7.                 //ExecutorService singlePool = Executors.newSingleThreadExecutor();
  8.                
  9.                 /**
  10.                  * Callable有返回值,而且会抛异常
  11.                  * 在线程池中用submit调用,返回一个Future
  12.                  */
  13.                 /*Future<String> future =
  14.                 singlePool.submit(new Callable<String>(){
  15.                         public String call() throws Exception {
  16.                                 Thread.sleep(2000);
  17.                                 return "hello";
  18.                         }
  19.                        
  20.                 });
  21.                
  22.                 try {
  23.                         System.out.println(future.get());  //通过get()方法得到结果
  24.                         System.out.println(future.get(1, TimeUnit.SECONDS)); //如果多少秒还没得到结果就抛异常
  25.                 } catch (InterruptedException e) {
  26.                         e.printStackTrace();
  27.                 } catch (ExecutionException e) {
  28.                         // TODO Auto-generated catch block
  29.                         e.printStackTrace();
  30.                 } catch (TimeoutException e) {
  31.                         // TODO Auto-generated catch block
  32.                         e.printStackTrace();
  33.                 }*/
  34.                 ExecutorService pool = Executors.newFixedThreadPool(3);
  35.                 /**
  36.                  * 等到一组Future,谁先返回,谁先加入到CompletionService的队列中
  37.                  */
  38.                 CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(pool);
  39.                 for(int i=0;i<10;i++){
  40.                         final int num = i;
  41.                         completionService.submit(new Callable<Integer>() {
  42.                                 public Integer call() throws Exception {
  43.                                         System.out.println("callable now is"+num);
  44.                                         return num;
  45.                                 }
  46.                         });
  47.                 }
  48.                 for(int i=0;i<10;i++){
  49.                         try {
  50.                                 System.out.println(completionService.take().get());
  51.                         } catch (InterruptedException e) {
  52.                                 e.printStackTrace();
  53.                         } catch (ExecutionException e) {
  54.                                 e.printStackTrace();
  55.                         }
  56.                 }
  57.         }
  58. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马