Java Callable用法

来源:互联网 发布:天猫淘宝商城童装女 编辑:程序博客网 时间:2024/06/02 11:49

原文:http://auguslee.iteye.com/blog/1292335

Callable 和 Runnable 的使用方法大同小异, 区别在于: 

1.Callable 使用 call() 方法, Runnable 使用 run() 方法 
2.call() 可以返回值, 而 run()方法不能返回。 
3.call() 可以抛出受检查的异常,比如ClassNotFoundException, 而run()不能抛出受检查的异常。 
Callable示例如下: 
Java代码  收藏代码
  1. class TaskWithResult implements Callable<String> {  
  2.     private int id;  
  3.   
  4.     public TaskWithResult(int id) {  
  5.         this.id = id;  
  6.     }  
  7.   
  8.     @Override  
  9.     public String call() throws Exception {  
  10.         return "result of TaskWithResult " + id;  
  11.     }  
  12. }  
  13.   
  14. public class CallableTest {  
  15.     public static void main(String[] args) throws InterruptedException,  
  16.             ExecutionException {  
  17.         ExecutorService exec = Executors.newCachedThreadPool();  
  18.         ArrayList<Future<String>> results = new ArrayList<Future<String>>();    //Future 相当于是用来存放Executor执行的结果的一种容器  
  19.         for (int i = 0; i < 10; i++) {  
  20.             results.add(exec.submit(new TaskWithResult(i)));  
  21.         }  
  22.         for (Future<String> fs : results) {  
  23.             if (fs.isDone()) {  
  24.                 System.out.println(fs.get());  
  25.             } else {  
  26.                 System.out.println("Future result is not yet complete");  
  27.             }  
  28.         }  
  29.         exec.shutdown();  
  30.     }  
  31. }  

执行结果 
Java代码  收藏代码
  1. result of TaskWithResult 0  
  2. result of TaskWithResult 1  
  3. result of TaskWithResult 2  
  4. result of TaskWithResult 3  
  5. result of TaskWithResult 4  
  6. result of TaskWithResult 5  
  7. result of TaskWithResult 6  
  8. result of TaskWithResult 7  
  9. result of TaskWithResult 8  
  10. result of TaskWithResult 9  

Runnable示例: 

Java代码  收藏代码
  1. public class LiftOff implements Runnable {  
  2.   
  3.     protected int countDown = 10;  
  4.     private static int taskCount = 0;  
  5.     private final int id = taskCount++;  
  6.   
  7.     public LiftOff() {  
  8.   
  9.     }  
  10.   
  11.     public LiftOff(int countDown) {  
  12.         this.countDown = countDown;  
  13.     }  
  14.   
  15.     public String status() {  
  16.         return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff! ") + ")";  
  17.     }  
  18.   
  19.     @Override  
  20.     public void run() {  
  21.         while (countDown-- > 0) {  
  22.             System.out.print(status());  
  23.             Thread.yield();  
  24.         }  
  25.         System.out.println();  
  26.     }  
  27.   
  28.     public static void main(String[] args) {  
  29.         ExecutorService exec = Executors.newFixedThreadPool(1);  
  30.         for (int i = 0; i < 5; i++) {  
  31.             exec.execute(new LiftOff());  
  32.         }  
  33.         exec.shutdown();  
  34.     }  
  35. }  

执行结果 
Java代码  收藏代码
  1. #0(9)#0(8)#0(7)#0(6)#0(5)#0(4)#0(3)#0(2)#0(1)#0(LiftOff! )  
  2. #1(9)#1(8)#1(7)#1(6)#1(5)#1(4)#1(3)#1(2)#1(1)#1(LiftOff! )  
  3. #2(9)#2(8)#2(7)#2(6)#2(5)#2(4)#2(3)#2(2)#2(1)#2(LiftOff! )  
  4. #3(9)#3(8)#3(7)#3(6)#3(5)#3(4)#3(3)#3(2)#3(1)#3(LiftOff! )  
  5. #4(9)#4(8)#4(7)#4(6)#4(5)#4(4)#4(3)#4(2)#4(1)#4(LiftOff! )  

注意ExecutorService 在Callable中使用的是submit(), 在Runnable中使用的是 execute()
1 0