【Thinking in Java】Java Callable的使用

  Callable<>和Runable相似,都是用于Java的并发执行。java

  惟一的区别是,Runable的run方法的返回是void,而Callable的call方法是有返回值的并发

  call方法返回的类型是实现Callable<?>泛型接口时所指定的类型,否则会编译出错。ide

  那么,怎样获取call方法的返回值呢?——经过执行Callable,能够返回的Future对象,经过调用future对象的get方法来获取call方法的返回值。this

  综上,你把Callable当成是有返回值的Runable就好了,只不过Callable的是call,Runable的是run。spa

  下面是演示代码:线程

 1 import java.util.ArrayList;
 2 import java.util.concurrent.Callable;
 3 import java.util.concurrent.ExecutionException;
 4 import java.util.concurrent.ExecutorService;
 5 import java.util.concurrent.Executors;
 6 import java.util.concurrent.Future;
 7 
 8 
 9 public class CallableTest{
10     public static void main(String[] args) {
11         ExecutorService executorService=    //建立线程池
12                 Executors.newCachedThreadPool();
13         ArrayList<Future<String>> list=
14                 new ArrayList<Future<String>>();
15         for (int i = 0; i < 10; i++) {
16             list.add(executorService    //经过submit方法来提交Callable到线程池中执行
17                     .submit(new TaskWithResult()));
18         }
19         for (Future<String> future : list) {
20             try {
21                 System.out.println(future.get()); //经过get方法来获取call返回值
22             } catch (InterruptedException e) {
23                 e.printStackTrace();
24             } catch (ExecutionException e) {
25                 e.printStackTrace();
26             }
27         }
28     }
29 
30     
31     static class TaskWithResult implements Callable<String>{
32         private static int taskNum=0;
33         private int taskId=taskNum;
34         public TaskWithResult() {
35             taskNum++;
36         }
37 
38         @Override
39         public String call() throws Exception {
40             try {
41                 Thread.sleep(this.taskId*1000);    //我制造的时间差,更直观地显示call和run方法的类似性。
42             } catch (InterruptedException e) {
43                 e.printStackTrace();
44             }
45             return "Result of TaskWithResult: "+this.taskId;
46         }
47         
48     } 
49 
50 }

 

  运行结果:code

  Result of TaskWithResult: 0
  Result of TaskWithResult: 1
  Result of TaskWithResult: 2
  Result of TaskWithResult: 3
  Result of TaskWithResult: 4
  Result of TaskWithResult: 5
  Result of TaskWithResult: 6
  Result of TaskWithResult: 7
  Result of TaskWithResult: 8
  Result of TaskWithResult: 9对象

  以上的运行结果是每隔一秒打印一行的,这说明,call和run是差很少的。blog

相关文章
相关标签/搜索