java 异步任务与结果
2016-06-22 22:49
615 查看
Callable, Future, FutureTask
Callable 与Runnable
Runnable 介绍Runnable只是一个接口,它可以被任何类继承,它的实例通过线程执行
Callable 与Runnable区别
代码
public interface Runnable { public abstract void run(); }
作用:
当做线程使用
当做任务被线程执行。
特点
当做任务没有时run()方法没有返回值;
不能抛出异常;
Callable 介绍
源码
public interface Callable<V> { V call() throws Exception; }
Callable是一个任务接口,它定义了一个带有返回值的并且抛出异常的任务。
作用
当做任务
特点
方法call() 具有返回值
可抛出异常,支持泛型
Future
future 介绍future 代表异步操作运算的结果,它也是一个接口;
常用于获取运行结果,查询是否完成,对任务结果进行取消
public interface Future<V> { /** 尝试取消当前任务,如果任务已经被完成,这个操作就失败返回fail, 参数 mayInterruptIfRunning 为ture 时,代表任务在执行中,也可以取消任务; 为false时,只能取消未被执行的任务。 */ boolean cancel(boolean mayInterruptIfRunning); /** * Returns {@code true} if this task was cancelled before it completed * normally. * * @return {@code true} if this task was cancelled before it completed */ boolean isCancelled(); /** 返回任务是否完成。 * * @return {@code true} if this task completed */ boolean isDone(); /** * 获取返回结果,有必要一直等在结果返回; */ V get() throws InterruptedException, ExecutionException; /** * 获取执行结果,有必要就等待timeout , 指定时间内结果没有执行完,返回Null * * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting * @throws TimeoutException if the wait timed out */ V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
功能
取消任务
获取任务执行结果
判断任务是否完成
FutureTask
简述FutureTask 是一个类,
部分源码
public class FutureTask<V> implements RunnableFuture<V> { ..... }
RunnableFuture
/** */ public interface RunnableFuture<V> extends Runnable, Future<V> { /** * Sets this Future to the result of its computation * unless it has been cancelled. */ void run(); }
RunnableFuture 继承了Runnable, 和Future,它既可以当做任务被执行,又可以判断结果是完成,但获取不到具体的值,因为它的父类是Callable 而不是Runnable。
FutureTask 是RunnableFuture 的子类实现。
示例
public class AsyncTask { public static void main(String args[]) throws InterruptedException, ExecutionException { ExecutorService service = Executors.newCachedThreadPool(); MyTask1 task1 = new MyTask1(); Future<String> result1 = service.submit(task1); System.out.println("...等待完成任务1。。执行结果是:" + result1.get()); MyTask2 task2 = new MyTask2(); Future<?> result2 = service.submit(task2); System.out.println("...等待完成任务2.。执行结果是。" + result2.get()); FutureTask<String> ft = new FutureTask<String>(task1); Future<String> reuslt2 = (Future<String>) service.submit(ft); System.out.println("...等待任务3.。执行结果是:" + result2.get()); service.shutdown(); } } class MyTask2 implements Runnable { @Override public void run() { System.out.println("..执行Runnable任务。。"); } } class MyTask1 implements Callable<String> { @Override public String call() throws Exception { System.out.println("...线程执行callable任务。"); return "success"; } }
执行结果
...线程执行callable任务。 ...等待完成任务1。。执行结果是:success ..执行Runnable任务。。 ...等待完成任务2.。执行结果是。null ...等待任务3.。执行结果是:null ...线程执行callable任务。
相关文章推荐
- ruby实现的一个异步文件下载HttpServer实例
- C#异步绑定数据实现方法
- 科学知识:同步、异步、阻塞和非阻塞区别
- 探讨Ajax中同步与异步之间的区别
- C#中异步回调函数用法实例
- C#实现异步GET的方法
- C#异步下载文件
- C#异步执行任务的方法
- 简单实现C#异步操作
- 使用Promise解决多层异步调用的简单学习心得
- 深入理解JavaScript编程中的同步与异步机制
- Jquery异步提交表单代码分享
- JQuery异步获取返回值中文乱码的解决方法
- jQuery通过deferred对象管理ajax异步
- Highcharts使用简例及异步动态读取数据
- Ajax异步上传文件实例代码分享
- Node.js 的异步 IO 性能探讨
- Ajax异步(请求)提交类 支持跨域
- Ajax实现异步用户名验证功能
- Ajax简单的异步交互及Ajax原生编写