参考自: https://www.cnblogs.com/linghu-java/p/8991824.html
Future接口代表异步计算的结果,通过future提供的方法可以查看异步计算是否执行完成,或者等待执行结果并获取执行结果,同时还可以取消执行。
futureTask表示的计算 是通过callable来实现的。有三种状态: 等待运行,正在运行,运行完成。执行完成 表示计算的所有可能结束方式,包括正常结束,由于取消而结束,由于异常而结束。
Future.get的行为取决于任务的状态。
FutureTask在Executor框架中表示异步任务,此外还可以用来表示一些时间较长的计算,这些计算可以在使用计算结果之前启动。
FutureTask是future的实现类。
FutureTask实现了RunnableFuture,runnableFuture接口继承自Runnable,Future
成员变量:
/** * 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. state表示 任务的运行状态,初始时是new。只有调用set,setException,和cancel方法里,运行状态才能过度到终止状态。 在完成任务期间,状态依赖在completing(当outcome被设置)或者被打断(只有当打断runner来满足一个取消事件)。从中间到最终状态的过度用便宜的有序的延迟写入。 因为这些值是唯一的。并且将来不能被更改的。 * * 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; /** The underlying callable; nulled out after running */ //潜在的运行线程,在运行结束之后被设置成null private Callable<V> callable; // 这个就是保存线程运行之后的返回值的。可以是正常的返回或者是异常信息 /** The result to return or exception to throw from get() */ private Object outcome; // non-volatile, protected by state reads/writes //实际运行callable的线程对象。 /** The thread running the callable; CASed during run() */ private volatile Thread runner; // /** Treiber stack of waiting threads */ private volatile WaitNode waiters; public void run() { // 判断状态是不是新的,如果是新的,那么则尝试把当前线程保存在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(); ran = true; } catch (Throwable ex) { //执行失败,在执行的过程中有异常,则result为null,ran为false,并且设置异常信息。 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); } } /** * Causes this future to report an {@link ExecutionException} * with the given throwable as its cause, unless this future has * already been set or has been cancelled. * 使future抛出给出的异常,除非future已经被设置了异常或者被取消了。 * <p>This method is invoked internally by the {@link #run} method * upon failure of the computation. * * @param t the cause of failure */ protected void setException(Throwable t) { //这句话的意思就是如果当前线程的状态是new,那么将new改成completing,并且返回true,就继续执行if括号内的代码,反之,则直接返回了,不执行if中的代码了。 if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { //将outcome设置成这个异常。 outcome = t; //就是将当前的状态改成异常状态。 UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state //设置成异常状态后,做完成状态的相关操作。 finishCompletion(); } } /** * Removes and signals all waiting threads, invokes done(), and * nulls out callable. 清空所有waitNode。并唤醒所有等待的线程,执行done方法。 */ private void finishCompletion() { // assert state > COMPLETING; for (WaitNode q; (q = waiters) != null;) { //判断当前的waitnode是不是q,如果是,那么将当前waitnode改成null,并返回true。 if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) { //通过自旋。就是死循环,来将waitNode中的线程都设置成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方法,done方法是个空方法,我们如果想自定义这个,可以覆盖这个方法。 done(); callable = null; // to reduce footprint } /** * Makes available the permit for the given thread, if it * was not already available. If the thread was blocked on * {@code park} then it will unblock. Otherwise, its next call * to {@code park} is guaranteed not to block. This operation * is not guaranteed to have any effect at all if the given * thread has not been started. * 如果给定的线程尚未可用,就使这个线程可用。 如果线程被阻塞在park方法,那么它就会变得不阻塞。否则,下一次call park方法时保证不会被阻塞。如果线程没有被启动,这个操作可以保证没有任何影响。 其实就是唤醒这个线程,不让它等待。 * @param thread the thread to unpark, or {@code null}, in which case * this operation has no effect */ public static void unpark(Thread thread) { if (thread != null) unsafe.unpark(thread); } /** * Protected method invoked when this task transitions to state * {@code isDone} (whether normally or via cancellation). The * default implementation does nothing. Subclasses may override * this method to invoke completion callbacks or perform * bookkeeping. Note that you can query status inside the * implementation of this method to determine whether this task * has been cancelled. 受保护的方法当任务改变状态到done时被调用,无论是正常的完成或者是被取消了。默认的实现是不做任何事。子类可以覆盖这个方法来调用完成的callback或者是执行记录。 注意你可以在这个方法实现的内部查询状态来决定这个任务是否被取消了。 */ protected void done() { } /** * Ensures that any interrupt from a possible cancel(true) is only * delivered to a task while in run or runAndReset. 保证从一个可能的取消事件的打断只能被发布到一个任务时当在运行时或者运行并且重置。 */ private void handlePossibleCancellationInterrupt(int s) { // It is possible for our interrupter to stall before getting a // chance to interrupt us. Let's spin-wait patiently. if (s == INTERRUPTING) while (state == INTERRUPTING) Thread.yield(); // wait out pending interrupt // assert state == INTERRUPTED; // We want to clear any interrupt we may have received from // cancel(true). However, it is permissible to use interrupts // as an independent mechanism for a task to communicate with // its caller, and there is no way to clear only the // cancellation interrupt. // // Thread.interrupted(); }