你的位置:首页 > 软件开发 > Java > Java Callable的使用

Java Callable的使用

发布时间:2015-05-08 00:01:06
Callable<>和Runable类似,都是用于Java的并发执行。  唯一的区别是,Runable的run方法的返回是void,而Callable的call方法是有返回值的。  call方法返回的类型是实现Callable<?>泛型接口时所指定的类型, ...

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

  唯一的区别是,Runable的run方法的返回是void,而Callable的call方法是有返回值的。

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

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

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

  下面是演示代码:

 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     @Override39     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 }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Java Callable的使用

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。