FutureTask注释
FutureTask属性
NORMAL 正常 2
EXCEPTIONAL 异常 3
CANCELLED 取消 4
INTERRUPTING 中断中 5
INTERRUNPED 被中断 6
NEW->CANCELED 被取消
NEW->INTERRUNPING->INTERRRUNPTED 被中断
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
FutureTask run()
public void run() {
1、
首先判断任务的状态 如果任务的状态不是new 说明任务的状态已经改变(说明他已经走了4种可能变化的一种)- 2、如果状态是new就会把 当前执行任务的线程付给runner, 这里用的cmpandset如果runner不为空 说明已经有线程在执行
- 3、任务也会退出执行,如果状态是new并且runner为空并且把当前的线程付给了runner那么就继续执行任务(runner state 都是 volatile 类型的变量是一个很轻量机的线程安全操作)
- 4、引起state状态变化的原因 就是调用了cancel 或是 run
if (state != NEW ||
!U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
//
如果 要执行的任务不为空 并且状态 new 就执行if (c != null && state == NEW) {
V result;
boolean ran;
try {
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);
}
}
protected void set(V v) {
//
如果state是new 把state设置成 COMPLETINGif (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
// 将结果保存到outcome
outcome = v;
U.putOrderedInt(this, STATE, NORMAL); // final state
finishCompletion();
}
}
protected void setException(Throwable t) {
//
如果state是new 把state设置成 COMPLETINGif (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
outcome = t;
U.putOrderedInt(this, STATE, EXCEPTIONAL); // final state
finishCompletion();
}
}
FutureTask cancel()
public boolean cancel(boolean mayInterruptIfRunning) {
// mayInterruptIfRunning 是否中断running
// 这个判断逻辑等价于:
//
if(state!=new || !UNSAFE.compareAndSwapInt(this, stateOffset, NEW, mayInterruptIfRunning ? INTERRUPTING : CANCELLED))- // 1、如果state不是new 那么就退出方法,这时的任务是已经完成了 或是被取消了 或是被中断了
- // 2、如果是state是new 就设置state 为中断状态 或是取消状态
if (!(state == NEW &&
U.compareAndSwapInt(this, STATE, NEW,
mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
return false;
try { // in case call to interrupt throws exception
if (mayInterruptIfRunning) {
try {
Thread t = runner;
if (t != null)
t.interrupt();
} finally { // final state
U.putOrderedInt(this, STATE, INTERRUPTED);
}
}
} finally {
finishCompletion();
}
return true;
}
- 如果Task已经在执行而callable.call()没有返回 或是 call()已经返回但是state状态还没有改变,那么任务调用cancel(false) 不会对任务的执行造成影响 只会影响task的状态 。
- 如果callable.call()已经返回并且状态已经变成COMPLETING或是 COMPLED 那么对任务执行 和任务状态都没有影响。
- 如果任务已经在执行而callable.call()没有返回,会把state设置成 INTERRUPTING然后调用执行线程的中断请求 然后把状态设置成INTERRUPTED,这里如果 callable.call()方法可以响应中断 可能对任务执行产生影响,如果方法不会响应中断不会对任务运行产生影响。影响任务的状态。
- 如果任务已经在执行并且 call()已经返回但是state状态还没有改变 不会对任务的执行造成影响 只会影响任务的状态 。
任务结果回调方法 done()
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (U.compareAndSwapObject(this, WAITERS, q, null)) {
for (;;) {
Thread t = q.thread;
if (t != null) {
q.thread = null;
LockSupport.unpark(t);
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
- // 子类覆写此方法,在计算结果结束后会回调此方法,获取结果的方法:get()
done();
callable = null; // to reduce footprint
}
获取结果的方法 get()
public V get() throws InterruptedException, ExecutionException {
int s = state;
// 如果没有计算完成,则等待
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
private V report(int s) throws ExecutionException {
// 获取到结果
Object x = outcome;
// 如果是正常,则返回结果
if (s == NORMAL)
return (V)x;
// 如果是异常,则抛出异常
CancellationExceptionif (s >= CANCELLED)
throw new CancellationException();
throw new ExecutionException((Throwable)x);
}