java中Callable接口是使用示例
2017-04-20 15:37
281 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ywf1214212153/article/details/70261619
与Runnable区别:
Runnable:没有返回信息,实现run()方法。
Callable<T>: A task that returns a result and may throw an exception ,返回执行结果,抛出异常;需要实现call()方法
Future
Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作。get方法会阻塞,直到任务返回结果。
FutureTask
FutureTask实现了Runnbale又实现了Futrue<V>这两个接口,既是Future、Runnable,又包装了Callable。
eg:
import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; public class CallableTest{ public static void main(String[] args) throws InterruptedException, ExecutionException { //1 。因此FutureTask既是Future、 Runnable,又是包装了Callable( 如果是Runnable最终也会被转换为Callable ), 它是这两者的合体。 FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() { @Override public Integer call() throws Exception { return new Random().nextInt(100); } }); new Thread(futureTask).start(); System.out.println(futureTask.get()); System.out.println("###########################################################"); //2 Executor就是Runnable和Callable的调度容器,Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作 ExecutorService threadPool = Executors.newSingleThreadExecutor(); Future<Integer> future = threadPool.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { return new Random().nextInt(100); } }); //get方法会阻塞,直到任务返回结果 System.out.println(future.get()); System.out.println("###########################################################"); //3 Future是按照添加的顺序排列的 ExecutorService threadPool1 = Executors.newSingleThreadExecutor(); List<Future<Integer>> list = new ArrayList<>(); for(int i=0;i<5;i++){ list.add(threadPool1.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { return new Random().nextInt(100); } })); } for(Future<Integer> fs:list){ System.out.println(fs.get()); } System.out.println("------------------------"); ArrayList<Callable<Integer>> arrayList = new ArrayList<Callable<Integer>>(); for(int i=0;i<5;i++){ arrayList.add(new Callable<Integer>() { @Override public Integer call() throws Exception { return new Random().nextInt(100); } }); } List<Future<Integer>> result = threadPool1.invokeAll(arrayList); for(Future<Integer> fs:result){ System.out.println(fs.get()); } System.out.println("###########################################################"); //4 提交到CompletionService中的Future是按照完成的顺序排列的 ExecutorService executorService = Executors.newSingleThreadExecutor(); CompletionService<Integer> cs = new ExecutorCompletionService<>(executorService); for(int i=0;i<5;i++){ final int id = i; cs.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { return id; } }); } for(int i=0;i<5;i++){ System.out.println(cs.take().get()); } System.out.println("###########################################################"); } }
相关文章推荐
- java自定义任务类定时执行任务示例 callable和future接口使用方法
- JAVA多线程实现的三种方式(继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程)
- java多线程返回值使用示例(callable与futuretask)
- java自定义任务类定时执行任务示例 callable和future接口使用方法
- Java使用PreparedStatement接口及ResultSet结果集的方法示例
- Java 中 ListIterator 接口的使用示例
- JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没
- JAVA中常用接口的介绍及使用示例:java.lang.Comparable
- [改善Java代码]异步运算考虑使用Callable接口
- Java多线程研究06-带返回值的线程定义接口Callable以及Future,FutureTask的使用
- 【示例教程】如何使用LEADTOOLS 的JAVA接口从护照中识别和提取数据
- java中接口(interface)及使用方法示例
- Java多线程Callable接口实现代码示例
- JAVA中常用接口的介绍及使用示例:java.lang.Comparable
- JAVA中常用接口的介绍及使用示例:java.lang.Comparable
- 使用Callable接口创建线程(Java5新增)
- MongoDB java api for 插入和单collection基本查询使用示例
- java 缓存框架java caching system使用示例
- 使用javah和javap得到JNI接口的签名
- Java多线程之Callable接口的实现