继续上一篇,我们来学习下Future体系。
开始
Callable
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
Callable接口和Runnable接口类似,只不过有返回值,可以抛出受检异常。
Future
Future表示一个异步任务的结果。
接口方法如下:
boolean cancel(boolean mayInterruptIfRunning)
该方法尝试取消任务的执行,当任务已执行完毕,或者已取消,或者其他原因不能取消时,返回false。如果成功取消,并且在调用cancel方法之前还没有start,则该任务将永不会运行。如果在调用cancel前,任务已经start,则根据参数mayInterruptIfRunning决定是否尝试中断运行该任务的线程。
调用cancel后,再调用isDone方法会永远返回true。
调用cancel并返回true后,再调用isCancelled方法会永远返回true。
boolean isCancelled()
如果任务正常执行完毕之前被取消掉,返回true。
boolean isDone()
任务执行完毕,或者抛出异常,或者被取消掉,返回true。
V get() throws InterruptedException, ExecutionException
若任务isDone,返回结果,这个结果可能是期望的运算结果 | 任务取消(CancellationException)| 任务中断(InterruptedException)| 其他异常(ExecutionException);否则阻塞当前线程,等待任务结束。
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
跟get()方法类似,只不过设置了最大等待时间,超时返回TimeoutException。
下面是一段伪代码,可以学习如何使用:
interface ArchiveSearcher {
String search(String target);
}
class App {
ExecutorService executor = ...
ArchiveSearcher searcher = ...
void showSearch(final String target) throws InterruptedException {
Future<String> future = executor.submit(new Callable<String>() {
public String call() {
return searcher.search(target);
}
});
displayOtherThings(); // do other things while searching
try {
displayText(future.get()); // use future
} catch (ExecutionException ex) {
cleanup();
}
}
}
RunnableFuture
没什么要介绍的,同时继承了Runnable和Future接口。
public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}
FutureTask
FutureTask是对Future接口的基本实现。一旦任务执行完毕,除非调用runAndReset方法(这是一个protected方法),否则不可以重新启动或者取消。
FutureTask有以下7种状态:
- NEW——任务创建、执行过程中均处于该状态。
- COMPLETING——瞬态
- NORMAL——任务正常执行完毕
- EXCEPTIONAL——任务抛出异常
- CANCELLED——任务取消
- INTERRUPTING——瞬态
- INTERRUPTED——任务被中断
可能的状态跳转如下:
- 正常执行完:NEW -> COMPLETING -> NORMAL
- 抛出异常:NEW -> COMPLETING -> EXCEPTIONAL
- 任务在start前取消:NEW -> CANCELLED
- 任务在start后取消:NEW -> INTERRUPTING -> INTERRUPTED
FutureTask有了两个构造方法:
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
第一个接收一个Callable参数。
第二个接收一个Runnable和一个任务成功执行完毕所需要的返回值作为参数。可以看到实际把Runnable包装成了Callable。
Future继承了Runnable接口,可以直接传递给Thread执行;其内部包含一个Callable的引用,实际对Callable.call()的调用正是在run()方法中。
Delayed
表示对一个对象的操作需要延迟一段时间执行。
public interface Delayed extends Comparable<Delayed> {
/**
* Returns the remaining delay associated with this object, in the
* given time unit.
*
* @param unit the time unit
* @return the remaining delay; zero or negative values indicate
* that the delay has already elapsed
*/
long getDelay(TimeUnit unit);
}
ScheduledFuture
public interface ScheduledFuture<V> extends Delayed, Future<V> {
}
RunnableScheduledFuture
public interface RunnableScheduledFuture<V> extends RunnableFuture<V>, ScheduledFuture<V> {
/**
* Returns {@code true} if this task is periodic. A periodic task may
* re-run according to some schedule. A non-periodic task can be
* run only once.
*
* @return {@code true} if this task is periodic
*/
boolean isPeriodic();//返回true,表明是周期性任务
}