java中Callable接口是使用示例
来源:互联网 发布:淘宝充流量 编辑:程序博客网 时间:2024/06/06 02:05
与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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic Integer call() throws Exception {return id;}});}for(int i=0;i<5;i++){System.out.println(cs.take().get());}System.out.println("###########################################################");}}
0 0
- java中Callable接口是使用示例
- Java 中 ListIterator 接口的使用示例
- Callable使用示例
- Callable接口的使用
- java的Callable接口
- java中的Callable接口
- Java多线程Callable接口
- 【Java】Callable 接口
- java中callable和futuretask使用
- JAVA中实现Callable接口来实现线程
- Callable 和Future 接口使用
- 使用Callable接口实现多线程
- Java接口:Callable 与 Future
- java 多线程callable接口实现
- Java Callable接口应用举例
- Java Callable接口应用举例
- java自定义任务类定时执行任务示例 callable和future接口使用方法
- java Callable使用实例
- jenkins+maven+svn实现简单的一键发布
- 二层交换机,三层交换机及四层交换机的区别
- ObjectMapper响应json数据异常的一种原因
- mysql优化
- 【OpenCV】将单通道的Mat对象转换为三通道的Mat
- java中Callable接口是使用示例
- [Oracle]oracle 监听器意外崩溃
- PHP7 变量结构变化
- ZOJ 2343 Robbers(思路)
- Git常用的命令
- Azure中配置网络安全组入站规则让外部网络访问VM中的网站
- HDU 4000 Fruit Ninja(树状数组)
- ubuntu 安装 libreoffice
- C# 2015关键字