Callable和Future实现调用任务并返回结果数据

1、Future取得的结果类型和Callable返回的结果类型必须一致,这是通过泛型来实现的。
   2、Callable要采用ExecutorSevice的submit方法提交,返回的future对象可以取消任务。
   3、CompletionService用于提交一组Callable任务,其take方法返回已完成的一个Callable任务对应的Future对象。
         (1) 好比我同时种了几块地的麦子,然后就等待收割。收割时,则是那块先成熟了,则先去收割哪块麦子。
  

   例子程序:

  

  1. package edu.java5.threadpool;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.Callable;  
  5. import java.util.concurrent.CompletionService;  
  6. import java.util.concurrent.ExecutionException;  
  7. import java.util.concurrent.ExecutorCompletionService;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.Future;  
  11.   
  12. public class CallableAndFuture {  
  13.   
  14.     public static void main(String[] args) {  
  15.          ExecutorService threadPool = Executors.newSingleThreadExecutor();  
  16.          /* 
  17.           * threadPool的execute(Runnable)方法调用不能返回结果,submit(Callable)方法调用可以返回结果, 
  18.           * 返回的结果封装在Future对象中 
  19.           */  
  20.          Future<String> future = threadPool.submit(new Callable<String>() {  
  21.             @Override  
  22.             public String call() throws Exception {  
  23.                 Thread.sleep(2000);  
  24.                 return "hello";  
  25.             }  
  26.          });  
  27.          System.out.println("等待结果...");  
  28.          //通过调用future.get()取得结果  
  29.          try {  
  30.             //future.get()下面的代码须取得后才能被执行  
  31.             System.out.println("取得结果:"+future.get());  
  32.             System.out.println("已经取得!");  
  33.         } catch (InterruptedException e) {  
  34.             e.printStackTrace();  
  35.         } catch (ExecutionException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.           
  39.         /** 
  40.          * CompletionService用于提交一组Callable任务,其take方法返回已完成的一个Callable任务对应的Future对象。  
  41.          **/  
  42.         ExecutorService threadPool2 = Executors.newFixedThreadPool(10);  
  43.         CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool2);  
  44.         for (int i = 0; i < 10; i++) {  
  45.             final int seq = i;  
  46.             completionService.submit(new Callable<Integer>() {  
  47.                 @Override  
  48.                 public Integer call() throws Exception {  
  49.                     Thread.sleep(new Random().nextInt(6000));  
  50.                     System.out.println(Thread.currentThread().getName()+" execute the Callable and return "+seq);  
  51.                     return seq;  
  52.                 }  
  53.             });  
  54.         }  
  55.         for (int i = 0; i < 10; i++) {  
  56.             try {  
  57.                 System.err.println(completionService.take().get());  
  58.             } catch (InterruptedException e) {  
  59.                 e.printStackTrace();  
  60.             } catch (ExecutionException e) {  
  61.                 e.printStackTrace();  
  62.             }  
  63.         }  
  64.         System.out.println("全部取完!");  
  65.     }  
  66.   
  67. }  
输出:
  1. 等待结果...  
  2. 取得结果:hello  
  3. 已经取得!  
  4. pool-2-thread-6 execute the Callable and return 5  
  5. 5  
  6. pool-2-thread-8 execute the Callable and return 7  
  7. 7  
  8. pool-2-thread-9 execute the Callable and return 8  
  9. 8  
  10. pool-2-thread-10 execute the Callable and return 9  
  11. 9  
  12. pool-2-thread-2 execute the Callable and return 1  
  13. 1  
  14. pool-2-thread-5 execute the Callable and return 4  
  15. 4  
  16. pool-2-thread-1 execute the Callable and return 0  
  17. 0  
  18. pool-2-thread-7 execute the Callable and return 6  
  19. 6  
  20. pool-2-thread-3 execute the Callable and return 2  
  21. 2  
  22. pool-2-thread-4 execute the Callable and return 3  
  23. 3  
  24. 全部取完! 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值