JDK内置的Future主要使用到了Callable接口和FutureTask类。Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务。Callable接口可以看作是Runnable接口的补充,Callbale含有泛型,相比Runnable接口的run()方法,call()方法带有返回值,并且可以抛出异常。
callable构造方法
@FunctionalInterface
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实例call()方法内部,Callable的泛型即为call()方法的返回值,再生成一个FutureTask的对象,FutureTask构造方法内部参数封装着Callable实例,然后把这个对象当作一个Runnable,作为参数传递给线程执行。
继承关系
public class FutureTask<V> implements RunnableFuture<V> {}
public interface RunnableFuture<V> extends Runnable, Future<V> {}
Future接口的方法:
public interface Future<V> {
//取消任务。参数:是否立即中断任务执行,或者等等任务结束
boolean cancel(boolean mayInterruptIfRunning);
//任务是否已经取消,若已取消,返回true
boolean isCancelled();
//任务是否已经完成。包括任务正常完成、抛出异常或被取消,都返回true
boolean isDone();
/*等待任务执行结束,获得V类型的结果。InterruptedException: 线程被中断异常, ExecutionException: 任务执行异常,如果任务被取消,还会抛出CancellationException*/
V get() throws InterruptedException, ExecutionException;
/*参数timeout指定超时时间,uint指定时间的单位,在枚举类TimeUnit中有相关的定义。如果计算超时,将抛出TimeoutException*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
- 调用不带参数的get方法的调用被阻塞,直到计算完成。如果在计算完成之前,调用带参get()方法超时时,会抛出TimeoutException异常。若运行该计算的线程被中断,两种get()方法都会抛出InterruptedException。如果计算已经完成,那么get方法立即返回。
- 若计算还在进行,isDone方法返回false;如果完成了,则返回true。
- 调用cancel()时,若计算还没有开始,它被取消且不再开始。若计算处于运行之中,那么如果mayInterrupt参数为true,它就被中断。
- 相比future.get(),其实更推荐使用get (long timeout, TimeUnit unit) 方法,因为设置了超时时间可以防止程序无限制的等待future的返回结果。
FutureTask源码解析
构造方法:
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
//状态为NEW
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 + result,继续看上面的第二个构造方法,看看Executors.callable(runnable, result)的实现:
public static <T> Callable<T> callable(Runnable task, T result) {
if (task == null)
throw new NullPointerException();
//new了一个RunnableAdapter,返回Callable,说明RunnableAdapter实现了Callable
return new RunnableAdapter<T>(task, result);
}
RunnableAdapter类是Executors的静态内部类
static final class RunnableAdapter<T> implements Callable<T> {
final Runnable task;
final T result;
RunnableAdapter(Runnable task, T result) {
this.task = task;
this.result = result;
}
public T call() {
//Runnable task执行了run()
task.run();
//返回了T result
return result;
}
}
状态值:
/* Possible state transitions:
* NEW -> COMPLETING -> NORMAL
* NEW -> COMPLETING -> EXCEPTIONAL
* NEW -> CANCELLED
* NEW -> INTERRUPTING -> INTERRUPTED
*/
private volatile int state;
//初始化状态
private static final int NEW = 0;
//正在执行
private static final int COMPLETING = 1;
//正常完成
private static final int NORMAL = 2;
//出现异常
private static final int EXCEPTIONAL = 3;
//被取消
private static final int CANCELLED = 4;
//正被中断
private static final int INTERRUPTING = 5;
//已被中断
private static final int INTERRUPTED = 6;
FutureTask的run方法:
public void run() {
/*compareAndSwapObject(this, runnerOffset,]null, Thread.currentThread()))
其中第一个参数为需要改变的对象,第二个为偏移量,第三个参数为期待的值,第四个为更新后的值。
*/
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
//call()方法是由FutureTask调用的,说明call()不是异步执行的
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
//设置异常
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
set方法:
protected void set(V v) {
// NEW -> COMPLETING
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
//返回结果,也包括异常
outcome = v;
//COMPLETING -> NORMAL
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
//唤醒等待的线程
finishCompletion();
}
}
run()方法解析就完成了,接下来就是get()方法获取返回结果了
public V get() throws InterruptedException, ExecutionException {
int s = state;
//是否是未完成状态,是则等待
if (s <= COMPLETING)
//等待过程
s = awaitDone(false, 0L);
return report(s);
}
/**
* @throws CancellationException {@inheritDoc}
*/
public V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
if (unit == null)
throw new NullPointerException();
int s = state;
if (s <= COMPLETING &&
(s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
throw new TimeoutException();
return report(s);
}