public void run() {
//. 条件一:不是初始状态
//。条件二:当前任务是否被其他线程占用
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 {
// 调用自己实现的callable, 或者装饰后的runnable
result = c.call();
ran = true;
} catch (Throwable ex) {
//。执行失败,自己实现的代码有bug
result = null;
ran = false;
//。给outcome 对象赋值
setException(ex);
}
//。如果正常完成,就给outcome 对象赋值
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) {
//。如果CAS 操作成功,避免此时线程被中断
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
//。outcome 赋值了输出结果
outcome = v;
//。更改state值
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
//。唤醒所有阻塞的get
finishCompletion();
}
}
//。报错执行
protected void setException(Throwable t) {
// 将状态从初始变成执行完毕, 防止中断
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = t;
// state 修改为返回异常
UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state
// 唤醒所有阻塞的get
finishCompletion();
}
}
最后执行的方法
private void finishCompletion() {
// assert state > COMPLETING;
//。waitNode 是一个先进后出的链表
for (WaitNode q; (q = waiters) != null;) {
//。吧waiters 设为null
if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
for (;;) {
// 获取线程
Thread t = q.thread;
if (t != null) {
//。help GC
q.thread = null;
// get方法会被park, 这时候进行唤醒
LockSupport.unpark(t);
}
WaitNode next = q.next;
// 链表尾部
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
// 自定义
done();
// help GC
callable = null; // to reduce footprint
}
run 方法:
get方法
public V get() throws InterruptedException, ExecutionException {
// 获取状态
int s = state;
// 如果处于初始, 未完成,正在执行, 调用get会被阻塞
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
// 当前线程被封装成WaitNode 对象
WaitNode q = null;
// 是否入队
boolean queued = false;
for (;;) {
// 如果处于中断
if (Thread.interrupted()) {
// 取出等待节点,抛出中断异常
removeWaiter(q);
throw new InterruptedException();
}
// 获取状态
int s = state;
//。如果已经执行完毕
if (s > COMPLETING) {
if (q != null)
// help GC
q.thread = null;
return s;
}
// 正在执行, 就让出CPU
else if (s == COMPLETING) // cannot time out yet
Thread.yield();
//。如果为null, 说明没有被创建过WaitNode 对象
else if (q == null)
q = new WaitNode();
//。入队, 链表的头插法,waiters 永远指向头节点
else if (!queued)
queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
q.next = waiters, q);
else if (timed) {
nanos = deadline - System.nanoTime();
if (nanos <= 0L) {
removeWaiter(q);
return state;
}
LockSupport.parkNanos(this, nanos);
}
// 阻塞当前线程
else
LockSupport.park(this);
}
}
// 正常返回就正常输出结果, 异常就抛出异常
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);
}
get 方法的流程:
中断
public boolean cancel(boolean mayInterruptIfRunning) {
// mayInterruptIfRunning 变量
// true 就CAS 让状态变为中断中
// false就是中断结束
if (!(state == NEW &&
UNSAFE.compareAndSwapInt(this, stateOffset, 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
UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
}
}
} finally {
// 最后都会去唤醒所有get方法等待的线程
finishCompletion();
}
return true;
}