java多线程 Callable与Future

一、 java.util.concurrent.Callable

Callable 接口 与 Runnable 接口类似,不同的是它们的唯一的 run 方法:

1、Callable 有返回值,Runnable 没有。
   Callable 的 run() 方法使用了 泛型类,可以返回任意类型的值。

2、Callable 抛出异常 ,Runnable 没有抛出。

同时 java.util.concurrent.Executors 提供了许多方法,可以操控 Callable 在线程池中运行。


二、java.util.concurrent.Future

Executors 执行 Callable 时会返回一个 Future 对象。使用 Future 我们可以得知 Callable 的运行状态,
以及获取 Callable 执行完后的返回值。

Future 的方法介绍:

    - get() :阻塞式,用于获取 Callable/Runnable 执行完后的返回值。
              带时间参数的get()重载方法用于最多等待的时间后,如仍未返回则线程将继续执行。

    - cancel() :撤销正在执行 Callable 的 Task。

    - isDone():是否执行完毕。

    - isCancelled():任务是否已经被取消。
  

三、使用实例

    100个任务各耗时 1 秒,用 10 个线程并行执行。

java代码

  1. import java.util.ArrayList;  
  2. import java.util.Date;  
  3. import java.util.List;  
  4. import java.util.concurrent.Callable;  
  5. import java.util.concurrent.ExecutionException;  
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8. import java.util.concurrent.Future;  
  9.   
  10. import org.junit.Test;  
  11.   
  12. public class T03_MyCallable implements Callable<String> {  
  13.   
  14.     @Override  
  15.     public String call() throws Exception {  
  16.         Thread.sleep(1000);  
  17.         return Thread.currentThread().getName();  
  18.     }  
  19.       
  20.       
  21.     @Test  
  22.     public void testName() throws Exception {  
  23.         ExecutorService executor = Executors.newFixedThreadPool(10);  
  24.         List<Future<String>> callableList = new ArrayList<Future<String>>();  
  25.         Callable<String> callable = new T03_MyCallable();  
  26.         for(int i=0; i< 100; i++){  
  27.             Future<String> future = executor.submit(callable);  
  28.             callableList.add(future);  
  29.         }  
  30.         for(Future<String> future : callableList){  
  31.             try {  
  32.                 System.out.println(new Date()+ "::"+future.get());//blocked.  
  33.             } catch (InterruptedException | ExecutionException e) {  
  34.                 e.printStackTrace();  
  35.             }  
  36.         }  
  37.         //shut down the executor service now  
  38.         executor.shutdown();  
  39.     }  
  40.       
  41.     /** 
  42.        NOTE: 
  43.        main thread should not complete earlier than sub thread, 
  44.        should at least wait for one task execution time in main thread, 
  45.        should use : future.get() method in main thread. 
  46.        or use: executor.awaitTermination(timeout, unit) in main thread. 
  47.      */  

结果

  1. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-1  
  2. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-2  
  3. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-3  
  4. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-4  
  5. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-5  
  6. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-6  
  7. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-7  
  8. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-8  
  9. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-9  
  10. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-10  
  11. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-1  
  12. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-3  
  13. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-5  
  14. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-7  
  15. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-9  
  16. ...  
  17. ...  
  18. ...  
  19. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-2  
  20. Sun Jan 08 22:01:55 CST 2017::pool-1-thread-4  
  21. Sun Jan 08 22:01:56 CST 2017::pool-1-thread-8  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值