简述
今天晚上闲来无事,所以想把FutureTask点出来瞧一瞧,每次看源码之前都是一件很痛苦的事情,其实看了一会儿之后就会发现其实整体流程很好理解的,我发现concurrent包下的都用的cas啊,看来这种不加锁的互斥真的效率非常高
什么是FutureTask
简单来说就是一个异步执行的,带返回值的一个方法,使用get方法可以获取返回的结果(如果线程没有执行完就阻塞在那里),我写了一个例子
public void doTask(){
Task myTask = new Task();
FutureTask<Integer> task = new FutureTask<Integer>(myTask);
ExecutorService service = Executors.newCachedThreadPool();
service.submit(task);
System.out.println("当前时间:"+System.currentTimeMillis());
try {
System.out.println("计算结果:"+task.get());
System.out.println("目前时间:"+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
class Task implements Callable<Integer> {
@Override
public Integer call() throws Exception {
Thread.sleep(2000);//模拟耗时操作
return 1;
}
}
其实提交到线程池之后,当有空闲的工作线程get到这个任务之后,就会执行该任务的run方法,我们可以看看FutureTask的run方法到底做了什么
run方法
public void run() {
首先利用cas的原子性将当前线程赋值给runner,如果操纵失败就说明有竞争,直接返回
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 {
result = c.call();//接下来这里很好理解吧,执行call方法,
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)//这里就表示了执行结束了,然后赋值,
set(result);
}
} finally {
//执行到这里就结束了,把runner标记为无人占有,
// 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) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
首先把state状态变成COMPLETING,表示已经完成(但还没赋值),之后把值赋给outcome,然后把state变成NORMAL,已完成,然后在finishCompletion使用 LockSupport.unpark(t);来唤醒线程,这里就不贴代码了。这样每个线程就可以拿回数啦,
那么在其他线程中怎么获取到值的呢?这就需要看一看get方法了
get
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
在FutureTask中维护了一个WaitNode链表,每次当一个线程调用get方法时,如果没有COMPLETING,那么就把它加入这个链表中
private int awaitDone(boolean timed, long nanos)
throws InterruptedException {
final long deadline = timed ? System.nanoTime() + nanos : 0L;
WaitNode q = null;
boolean queued = false;
for (;;) {
if (Thread.interrupted()) {
removeWaiter(q);
throw new InterruptedException();
}
int s = state;
if (s > COMPLETING) {
if (q != null)
q.thread = null;
return s;
}
else if (s == COMPLETING) // cannot time out yet
Thread.yield();
else if (q == null)
q = new WaitNode();
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);
}
}
添加之后我们就可以看到,如果状态是大于COMPLETING,就说明完成了,直接返回,如果走到else的话就会
使用LockSupport.park()来阻塞线程