读前必看AQS原理——http://blog.csdn.net/qq_31957747/article/details/74910939
一、Callable和Future
对比Callable和Runnable:
Runnable接口:
public interface Runnable {
public abstract void run();
}
Callable接口:
public interface Callable<V> {
V call() throws Exception;
}
两者的不同在于:
1、Runnable接口的run()方法没有返回值,而Callable接口的call()方法是带有泛型的返回值。
2、Runnable方法的run()方法的异常只能在内部处理,而不能向上抛,而Callable的call()方法允许抛出异常。
Future:表示异步计算的结果,它提供了检查计算是否完成的方法,以等待计算的完成,并接收计算的结果。Future的cancel()方法可以取消任务的执行,它有一布尔参数,参数为 true 表示立即中断任务的执行,参数为 false 表示允许正在运行的任务运行完成。Future的 get() 方法等待计算完成,获取计算结果。
Callable的Demo:
public class TestCallable {
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
FutureTask task = new FutureTask(td);
new Thread(task).start();
try {
Thread.sleep(1000);//执行其他操作
System.out.println(task.get());//等待计算结果
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class ThreadDemo implements Callable<Integer>{
@Override
public Integer call() throws Exception {
int sum = 0;
for(int i = 0;i<=100;i++){
sum += i;
}
return sum;
}
}
二、FutrueTask源码分析:
UML图:
FutrueTask的构造方法:
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
sync = new Sync(callable);
}
public FutureTask(Runnable runnable, V result) { //把Runnable适配成Callable
sync = new Sync(Executors.callable(runnable, result));
}
可以看到FutrueTask中也有一个继承了AQS的内部类Sync。
看看Sync的成员变量:
//下面是任务的四个状态值,使用AQS的state来表示,默认为0
private static final int READY = 0; //任务准备执行
private static final int RUNNING = 1; //任务正在执行
private static final int RAN = 2; //已经执行完毕
private static final int CANCELLED = 4; //任务被取消
private final Callable<V> callable;
//get()方法得到的结果
private V result;
//get()方法抛出的异常
private Throwable exception;
//当前任务执行的线程对象
private volatile Thread runner;
FutureTask的get()方法:
public V get() throws InterruptedException, ExecutionException {
return sync.innerGet();
}
然后看Sync的innerGet()方法:
V innerGet() throws InterruptedException, ExecutionException {
acquireSharedInterruptibly(0); //AQS共享模式的可中断的获取资源的方法,不过参数为0
if (getState() == CANCELLED) //任务被取消,抛出异常
throw new CancellationException();
if (exception != null) //call方法有异常抛出
throw new ExecutionException(exception);
return result;
}
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0) //任务是否完成
doAcquireSharedInterruptibly(arg);//任务没完成,排队等待
}
doAcquireSharedInterruptibly在AQS那篇已经讲过,就是个排队等待的过程,看看重写后的tryAcquireShared:
protected int tryAcquireShared(int ignore) {
return innerIsDone() ? 1 : -1;
}
boolean innerIsDone() {
return ranOrCancelled(getState()) && runner == null;//状态为RAN或CANCELLED,且当前没有要执行的线程对象
}
判断任务是否完成,若完成(即任务状态为RAN或CANCELLED,且当前没有要执行的线程对象),否则,进入等待状态。
通过上面的分析,我们看到get()方法跟任务的状态有很大关系,那么任务的状态是怎么被设置的呢。
下面我们看run()方法:
public void run() {
sync.innerRun();
}
调用了sync的innerRun()方法。
void innerRun() {
if (!compareAndSetState(READY, RUNNING)) //CAS设置状态,预期值READY,更新值RUNING,设置失败函数直接返回
return;
runner = Thread.currentThread(); //拿到当前线程
if (getState() == RUNNING) { // 上面把state设置成了RUNNING,这里重新检查
V result;
try {
result = callable.call(); //调用Callable的call方法取到返回值
} catch (Throwable ex) {
setException(ex);
return;
}
set(result); //将返回值设置给result,并设置状态
} else {
releaseShared(0); //这边是CANCELLED状态
}
}
看看set()方法:
protected void set(V v) {
sync.innerSet(v);
}
void innerSet(V v) {
for (;;) { //自旋
int s = getState(); //拿到任务状态
if (s == RAN)
return;
if (s == CANCELLED) {
releaseShared(0);
return;
}
if (compareAndSetState(s, RAN)) {//CAS设置状态,预期值RUNNING,更新值RAN
result = v;
releaseShared(0);
done(); //done方法为空方法可以重写,相当于一个回调函数
return;
}
}
}
再看releaseShared()方法:
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
doReleaseShare()方法就是唤醒在等待的get()方法的线程。我们看FutureTask中多tryReleaseShare()方法的重写:
protected boolean tryReleaseShared(int ignore) {
runner = null; //将当前任务执行的线程对象设置为null
return true;
}
总结一下run方法:
将状态从默认的READY变成RUNNING,然后调用Callable的call方法,并取到返回值,然后把状态从RUNNING设置成RAN,并唤醒get()方法的等待线程。