FutureTask类
原作者:源码笔记
FutureTask实现RunnableFutrue。RunnableFutrue接口又继承Future和Runnable接口。所以,FutureTask既可以提交到线程异步执行,也可以通过Futrue接口获取结果。Future接口相当于当前任务的凭证,可以通过Future获取当前任务的执行情况。
FutrueTask的属性变量及构造函数
int state状态。
(1)正常情况状态流转。NEW–>COMPLETING–>NORMAL
(2)异常情况状态流转。NEW -> COMPLETING -> EXCEPTIONAL
(3)取消,中断。
NEW -> CANCELLED 或 NEW -> INTERRUPTING -> INTERRUPTED
Callable callable变量。记录CallAble接口的实现类
Object outcome变量。Object类型的返回对象。
Thread runner变量。执行任务的线程对象。
WaitNode waiters变量。get方法被阻塞的线程封装的节点对象。这是一个单链表结构。
有两个构造函数。一个是入参为Callable callable的构造函数。callable赋值给变量callable。如下
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
另一个构造函数,入参为Runnable runnable, V result。通过Executors.callable方法,将runnable转换为callable。result作为callable正常执行结束的返回值。
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
public class FutureTask<V> implements RunnableFuture<V> {
/**
* 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;
/** The underlying callable; nulled out after running */
private Callable<V> callable;
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
/** The thread running the callable; CASed during run() */
private volatile Thread runner;
/** Treiber stack of waiting threads */
private volatile WaitNode waiters;
/**
* Returns result or throws exception for completed task.
*
* @param s completed state value
*/
@SuppressWarnings("unchecked")
private V report(int s) throws ExecutionException {
Object x = outcome;
if (s == NORMAL)
return (V)x;
if (s >= CANCELLED)
throw new CancellationException();
throw new ExecutionException((Throwable)x);
}
/**
* Creates a {@code FutureTask} that will, upon running, execute the
* given {@code Callable}.
*
* @param callable the callable task
* @throws NullPointerException if the callable is null
*/
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
/**