我画了一张关于FutureTask的类图,主要包括FutureTask的几个重要的函数和字段,还有它和父类的关系。

根据上面图我们可以清晰的看出FutureTask的继承关系。FutureTask继承一个最重要的类是Future,有几个比较重要的方法get,cancel,isCancelled等。FutureTask是为了弥补Thread的不足而设计的,它可以让程序员准确地知道线程什么时候执行完成并获得到线程执行完成后返回的结果(如果有需要)。FutureTask是一种可以取消的异步的计算任务。它的计算是通过Callable实现的,它等价于可以携带结果的Runnable,并且有三个状态:等待、运行和完成。完成包括所有计算以任意的方式结束,包括正常结束、取消和异常。
```java
public class FutureTaskTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
FutureTask futureTask = new FutureTask(new Callable() {
public Integer call() throws Exception {
System.out.println("执行一个比较耗时的任务");
Thread.sleep(3000);
int sum = 0;
for(int i=0;i System.out.println("主线程....");

try {
futureTask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

System.out.println("所有的任务执行完毕....");
}

}

从上面的例子中我们可以看到FutureTask在执行异步的时候是需要依托线程池的。也就是调用 Executors.newCachedThreadPool(),submit是把futureTask这个Task交给线程池,我们可以看下AbstractExecutorService的submit:
```java
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}

然后执行execute,这个方法是Executor接口中的唯一方法

public interface Executor {
void execute(Runnable command);
}

方法是实现是在ThreadPollExecutor,这个方法主要是从线程池中获取一个线程来执行call任务,具体的逻辑我暂时还不是很清楚,因为这块源码我还没有研究过,有空的时候再研究下到时候更新下这篇文章。

 public void execute(Runnable command) {
if (command == null)
throw new NullPointerException(); int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}

下面是FutureTask的源码分析,futureTask初始化

public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}

state是一个volatile的变量,用线程的状态

/**
* The run state of this task, initially NEW. The run state
* transitions to a terminal state only in methods set,
* setException, and cancel. During completion, state may take on
* transient values of COMPLETING (while outcome is being set) or
* INTERRUPTING (only while interrupting the runner to satisfy a
* cancel(true)). Transitions from these intermediate to final
* states use cheaper ordered/lazy writes because values are unique
* and cannot be further modified.
*
* 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;

注释已经写得很清楚了,初始化的时候是NEW,Callable的call执行完就变为COMPLETING没有异常就再改为 NORMAL,否则就是EXCEPTIONAL

当线程开始执行的时候会先执行run函数。那么一般的我们的task会在初始化的时候new一个Callable对象,并重写call函数。所以在run的时候是会执行call这个函数。看下call代码就一目了然了。

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 {
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);
}
}

所以当call还没执行完我们的主线程会继续执行直到遇到get方法。get会执行阻塞,那么call什么时候才会被唤醒呢,这个就需要操作系统的支持了,这里的处理方式跟AQS一样是托管给操作系统,先把线程挂起,等待操作系统唤醒,那么get这个方法到底做了什么处理呢?

public V get() throws InterruptedException, ExecutionException {
int s = state;
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 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);
}
}

这个waitDone最重要的职能有两点一个是把当前线程的WaitNode赋值给waiters,通过CAS。二是把这个线程阻塞,通过LockSupport.park,直到call执行完毕,那么run就会执行set方法,set主要是修改state的状态:

protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (UNSAFE.compareAndSwapObject(this, waitersOffset, 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;
}
} done(); callable = null; // to reduce footprint
}

有意思的是FutureTask预留了一个done函数给它的子类用,有需要的开发者可以对这个函数进行重写。

protected void done() { 

参考:

http://ifeve.com/abstractqueuedsynchronizer-use/#more-18899

jdk1.8 J.U.C之FutureTask实现机制分析的更多相关文章

  1. 【并发编程】【JDK源码】J.U.C--组件FutureTask、ForkJoin、BlockingQueue

    原文:慕课网实战·高并发探索(十三):并发容器J.U.C -- 组件FutureTask.ForkJoin.BlockingQueue FutureTask FutureTask是J.U.C中的类,是 ...

  2. java中Future与FutureTask使用与分析

    Future与FutureTask都是用于获取线程执行的返回结果.下面我们就对两者之间的关系与使用进行一个大致的介绍与分析 一.Future与FutureTask介绍: Future位于java.ut ...

  3. FutureTask 源码分析

    FutureTask 源码分析,这个类的原理与我分析android当中的FutureTask类差不多[http://www.cnblogs.com/daxin/p/3802392.html] publ ...

  4. 并发容器J.U.C --组件FutureTask、ForkJoin、BlockingQueue

    FutureTask FutureTask是J.U.C中的类,是一个可删除的异步计算类.这个类提供了Future接口的的基本实现,使用相关方法启动和取消计算,查询计算是否完成,并检索计算结果.只有在计 ...

  5. 一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue

    摘要: J.U.C是Java并发编程中非常重要的工具包,今天,我们就来着重讲讲J.U.C里面的FutureTask.Fork/Join框架和BlockingQueue. 本文分享自华为云社区<[ ...

  6. Java并发编程笔记之FutureTask源码分析

    FutureTask可用于异步获取执行结果或取消执行任务的场景.通过传入Runnable或者Callable的任务给FutureTask,直接调用其run方法或者放入线程池执行,之后可以在外部通过Fu ...

  7. 并发编程—— FutureTask 源码分析

    1. 前言 当我们在 Java 中使用异步编程的时候,大部分时候,我们都会使用 Future,并且使用线程池的 submit 方法提交一个 Callable 对象.然后调用 Future 的 get ...

  8. FutureTask源码分析

    1. 常量和变量 private volatile int state; // 任务状态 private static final int NEW = 0; private static final ...

  9. 并发编程-Future+callable+FutureTask 闭锁机制

    项目中经常有些任务需要异步(提交到线程池中)去执行,而主线程往往需要知道异步执行产生的结果,这时我们要怎么做呢?用runnable是无法实现的,我们需要用callable实现. FutureTask ...

随机推荐

  1. IIS 日志文件分析

    先安装下文参考资料中的log parser studio 然后就可以针对日志文件进行sql语句的查询了. 各页面访问量排行 ) FROM '[LOGFILEPATH]' where cs-uri-st ...

  2. Untracked files不想add

    $ git status On branch feature/20160420_complain_630222 Untracked files: (use "git add <file ...

  3. npm 发布包

    每个包都必须包含package.json配置文件 生成package.page文件 1.到项目目录下执行npm init根据提示输入即可 最后记得要yes 完成项目后就是要发布到npm了 首先需要有n ...

  4. CORS Cookie,和nodejs中的具体实现

    CORS(跨来源资源共享协议),高级浏览器(Chrome,firefox, opera, safir, ie10)在 XMLHttpRequest(AJAX) 中已经支持了这个协议.可以实现ajax跨 ...

  5. Flash Media Server 4.5 序列号 (fms4.5 激活码)

    激活码一枚   ,网上找不到的..我今天放出来了哦... 1462-5864-7783-6034-8316-3718    (亲测 可用) 安装前找到系统盘下windows/system32/driv ...

  6. DevExpress.XtraGrid winform试用分享

    DevExpress.XtraGrid在winform里使用还挺麻烦,为了减少重复代码和代码复用,本人针对DevExpress.XtraGrid封装了一个Form的基类,其中涵盖了DevExpress ...

  7. 科谱,如何单机环境下合理的备份mssql2008数据库

    前言: 终于盼来了公司的自用服务器:1U.至强CPU 1.8G 4核.16G内存.500G硬盘 X 2 (RAID1);装了64位win2008,和64位mssql2008.仔细把玩了一天把新老业务系 ...

  8. python watchdog

    监视文件变更 #!/usr/bin/python # -*- coding:UTF-8 -*- import time from watchdog.observers import Observer ...

  9. [CS231n-CNN] Training Neural Networks Part 1 : activation functions, weight initialization, gradient flow, batch normalization | babysitting the learning process, hyperparameter optimization

    课程主页:http://cs231n.stanford.edu/   Introduction to neural networks -Training Neural Network ________ ...

  10. 解决访问StackOverFlow太慢的问题

    Stackoverflow加载时访问了被屏蔽的站点ajax.googleapis.com,导致加载缓慢,把这个站点加到Hosts里,指向127.0.0.1即可