【Thinking in Java】Java Callable的使用
2015-05-07 22:23
120 查看
Callable<>和Runable类似,都是用于Java的并发执行。
唯一的区别是,Runable的run方法的返回是void,而Callable的call方法是有返回值的。
call方法返回的类型是实现Callable<?>泛型接口时所指定的类型,不然会编译出错。
那么,怎样获取call方法的返回值呢?——通过执行Callable,可以返回的Future对象,通过调用future对象的get方法来获取call方法的返回值。
综上,你把Callable当成是有返回值的Runable就行了,只不过Callable的是call,Runable的是run。
下面是演示代码:
运行结果:
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是差不多的。
唯一的区别是,Runable的run方法的返回是void,而Callable的call方法是有返回值的。
call方法返回的类型是实现Callable<?>泛型接口时所指定的类型,不然会编译出错。
那么,怎样获取call方法的返回值呢?——通过执行Callable,可以返回的Future对象,通过调用future对象的get方法来获取call方法的返回值。
综上,你把Callable当成是有返回值的Runable就行了,只不过Callable的是call,Runable的是run。
下面是演示代码:
import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CallableTest{ public static void main(String[] args) { ExecutorService executorService= //创建线程池 Executors.newCachedThreadPool(); ArrayList<Future<String>> list= new ArrayList<Future<String>>(); for (int i = 0; i < 10; i++) { list.add(executorService //通过submit方法来提交Callable到线程池中执行 .submit(new TaskWithResult())); } for (Future<String> future : list) { try { System.out.println(future.get()); //通过get方法来获取call返回值 } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } static class TaskWithResult implements Callable<String>{ private static int taskNum=0; private int taskId=taskNum; public TaskWithResult() { taskNum++; } @Override public String call() throws Exception { try { Thread.sleep(this.taskId*1000); //我制造的时间差,更直观地显示call和run方法的相似性。 } catch (InterruptedException e) { e.printStackTrace(); } return "Result of TaskWithResult: "+this.taskId; } } }
运行结果:
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是差不多的。
相关文章推荐
- thinking in java 附录A 使用非JAVA代码
- java中的Callable和Future的使用和困惑
- final关键字的使用(Thinking In Java)
- java线程:关于Callable和Future的简单使用:有返回值的线程
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- java并发--Callable的使用
- java中callable和futuretask使用
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- 【Thinking in Java笔记】Java中判断使用继承还是组合
- java 使用Callable和Future返回线程执行的结果
- java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5
- 【Thinking in Java笔记】怎么使用protected关键字
- Java:多线程,线程池,使用CompletionService通过Future来处理Callable的返回结果
- JAVA多线程实现的三种方式(继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程)
- JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没
- Java—Callable,Future的使用
- Thinking in Java学习笔记,使用Exchanger交换资源
- java多线程学习笔记:使用Callable和Future
- java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5
- Java并发编程:Callable和Future使用