在 Java 中,Runnable 接口表示一个没有返回结果的任务,而 Callable 接口表示具备返回结果的任务。
在并发编程中,异步执行任务,再获取任务结果,能够提升系统的吞吐量。Future 接口应运而生,它表示异步任务的执行结果,并提供了检查任务是否执行完、取消任务、获取任务执行结果等功能。FutureTask 是 Future 接口的基本实现,常与线程池实现类 ThreadPoolExecutor 配合使用。java
本文基于 jdk1.8.0_91
RunnableFuture 接口同时实现了 Runnable 接口和 Future 接口,是一种冗余设计。node
java.util.concurrent.RunnableFuture编程
/** * A {@link Future} that is {@link Runnable}. Successful execution of * the {@code run} method causes completion of the {@code Future} * and allows access to its results. * * @see FutureTask * @see Executor * @since 1.6 * @author Doug Lea * @param <V> The result type returned by this Future's {@code get} method */ public interface RunnableFuture<V> extends Runnable, Future<V> { /** * Sets this Future to the result of its computation * unless it has been cancelled. */ void run(); }
FutureTask 是一个可取消的异步任务,是对 Future 接口的基本实现,具备如下功能:segmentfault
同时,FutureTask 能够用于包装 Callable 或 Runnable 对象。
因为它实现了 Runnable 接口,能够提交给 Executor 执行。安全
/** * A cancellable asynchronous computation. * * @since 1.5 * @author Doug Lea * @param <V> The result type returned by this FutureTask's {@code get} methods */ public class FutureTask<V> implements RunnableFuture<V>
java.util.concurrent.Executor并发
/** * An object that executes submitted {@link Runnable} tasks. * * @since 1.5 * @author Doug Lea */ public interface Executor { void execute(Runnable command); }
java.util.concurrent.FutureTaskless
// The run state of this task, initially NEW. // 任务的执行状态,初始为 NEW。 private volatile int state; /** The underlying callable; nulled out after running */ // 须要执行的任务,任务执行完后为空 private Callable<V> callable; /** The result to return or exception to throw from get() */ // 任务的执行结果,或者任务抛出的异常 private Object outcome; // non-volatile, protected by state reads/writes /** The thread running the callable; CASed during run() */ // 执行任务的线程 private volatile Thread runner; /** Treiber stack of waiting threads */ // 指向栈顶的指针,栈结构用于存储等待任务执行结果的线程 private volatile WaitNode waiters;
其中 state、runner、waiters 三个属性在并发时存在争用,采用 CAS 维护其准确性。异步
// Unsafe mechanics private static final sun.misc.Unsafe UNSAFE; private static final long stateOffset; private static final long runnerOffset; private static final long waitersOffset; static { try { UNSAFE = sun.misc.Unsafe.getUnsafe(); Class<?> k = FutureTask.class; stateOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("state")); runnerOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("runner")); waitersOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("waiters")); } catch (Exception e) { throw new Error(e); } }
/** * 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;
FutureTask 中使用 state 表明任务在运行过程当中的状态。随着任务的执行,状态将不断地进行转变。async
状态的说明:ide
状态转移过程:
NEW -> COMPLETING -> NORMAL NEW -> COMPLETING -> EXCEPTIONAL NEW -> CANCELLED NEW -> INTERRUPTING -> INTERRUPTED
状态的分类:
FutureTask 中判断任务是否已取消、是否已完成,是根据 state 来判断的。
public boolean isCancelled() { return state >= CANCELLED; // CANCELLED、INTERRUPTING、INTERRUPTED } public boolean isDone() { return state != NEW; }
能够看到:
根据状态值,FutureTask 能够保证已经完成的任务不会被再次运行或者被取消。
中间状态虽然是一个瞬时状态,在 FutureTask 中用于线程间的通信。例如:
/** Treiber stack of waiting threads */ private volatile WaitNode waiters; // 栈顶指针 /** * Simple linked list nodes to record waiting threads in a Treiber * stack. See other classes such as Phaser and SynchronousQueue * for more detailed explanation. */ static final class WaitNode { volatile Thread thread; // 等待任务执行结果的线程 volatile WaitNode next; // 栈的下一个节点 WaitNode() { thread = Thread.currentThread(); } }
FutureTask 使用链表来构造栈(Treiber stack,使用 CAS 保证栈操做的线程安全,参考 java.util.concurrent.SynchronousQueue.TransferStack)。
其中 waiters 是链表的头节点,表明栈顶的指针。
栈的做用:
FutureTask 实现了 Future 接口,若是获取结果时,任务尚未执行完毕,那么获取结果的线程就在栈中挂起,直到任务执行完毕被唤醒。
赋值任务,设置任务的初始状态。
/** * Creates a {@code FutureTask} that will, upon running, execute the * given {@code Callable}. * * @param callable the callable task * @throws NullPointerException if the callable is null */ public FutureTask(Callable<V> callable) { if (callable == null) throw new NullPointerException(); this.callable = callable; this.state = NEW; // ensure visibility of callable } /** * Creates a {@code FutureTask} that will, upon running, execute the * given {@code Runnable}, and arrange that {@code get} will return the * given result on successful completion. * * @param runnable the runnable task * @param result the result to return on successful completion. If * you don't need a particular result, consider using * constructions of the form: * {@code Future<?> f = new FutureTask<Void>(runnable, null)} * @throws NullPointerException if the runnable is null */ public FutureTask(Runnable runnable, V result) { this.callable = Executors.callable(runnable, result); this.state = NEW; // ensure visibility of callable }
值得注意的两个地方:
测试:
@Test public void executors() throws Exception { Callable<String> callable = Executors.callable(new Runnable() { @Override public void run() { System.out.println("run!"); } }, "haha"); String call = callable.call(); System.out.println("call = " + call); }
执行结果:
run! call = haha
代码流程:
注意,在任务执行结束后,属性 runner、callable 都会被清空。
java.util.concurrent.FutureTask#run
public void run() { // state != NEW 说明任务已经执行完毕,再也不重复执行 // 将 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(); // 执行 Callable#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) // INTERRUPTING、INTERRUPTED handlePossibleCancellationInterrupt(s); } }
任务执行成功以后,调用该方法。
用于设置任务状态、设置任务执行结果、唤醒栈中等待任务执行结果的线程。
java.util.concurrent.FutureTask#set
/** * Sets the result of this future to the given value unless * this future has already been set or has been cancelled. * * <p>This method is invoked internally by the {@link #run} method * upon successful completion of the computation. * * @param v the value */ protected void set(V v) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { // state: NEW -> COMPLETING outcome = v; UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state: COMPLETING -> NORMAL finishCompletion(); } }
状态变化:NEW -> COMPLETING -> NORMAL
因为 state 属性是 volatile,这里 putOrderedInt 和 putIntVolatile 是等价的,保证可见性。
为何这里使用 lazySet 而没有用 CAS :
任务执行发生异常,调用该方法。
除了设置任务状态不一样,其余与 FutureTask#set 相同。
状态变化:NEW -> COMPLETING -> EXCEPTIONAL
java.util.concurrent.FutureTask#setException
/** * 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. * * <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) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { // state: NEW -> COMPLETING outcome = t; UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state: COMPLETING -> EXCEPTIONAL finishCompletion(); } }
执行完毕,唤醒等待线程。
java.util.concurrent.FutureTask#finishCompletion
/** * Removes and signals all waiting threads, invokes done(), and * nulls out callable. */ private void finishCompletion() { // assert state > COMPLETING; for (WaitNode q; (q = waiters) != null;) { if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) { // CAS 将 waiters 属性置空:1. CAS 成功,遍历链表唤醒全部节点;2. CAS 失败,从新读取 waiters 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#cancel 方法中,会先将 state 设为 INTERRUPTING,再中断 runner 线程,最后将 state 设为 INTERRUPTED。
因此在 FutureTask#run 的 finally 块中若是检查到 state == INTERRUPTING
,说明其余线程发起了 cancel(true) 操做,这里须要等待其余线程中断当前线程。直到检测到 state != INTERRUPTING
,说明其余线程已完成中断当前线程操做。
java.util.concurrent.FutureTask#handlePossibleCancellationInterrupt
/** * 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) // 其余线程中断当前线程以后,会设置 state 为 INTERRUPTED,使这里结束循环 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(); }
支持周期性执行任务:
/** * designed for use with tasks that intrinsically execute more // 设计用来支持定时任务 * than once. * * @return {@code true} if successfully run and reset */ protected boolean runAndReset() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return false; boolean ran = false; int s = state; try { Callable<V> c = callable; if (c != null && s == NEW) { try { c.call(); // don't set result ran = true; } catch (Throwable ex) { setException(ex); // 修改 state: NEW -> COMPLETING -> EXCEPTIONAL } } } 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 s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } return ran && s == NEW; // 返回 true 则容许下次再执行 runAndReset }
获取任务执行的结果:
java.util.concurrent.FutureTask#get()
/** * @throws CancellationException {@inheritDoc} */ public V get() throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone(false, 0L); // 自旋或阻塞等待任务完成 return report(s); // 获取任务执行结果或抛出异常 }
等待任务完成(任务执行完成、任务执行出现异常、任务取消执行),若当前线程发生中断、超时则中止等待。
在自旋中进行判断:
若 state < COMPLETING,须要将当前线程入栈等待:
java.util.concurrent.FutureTask#awaitDone
/** * Awaits completion or aborts on interrupt or timeout. * * @param timed true if use timed waits * @param nanos time to wait, if timed * @return state upon completion */ 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(); // 建立节点,设置 q.thread else if (!queued) queued = UNSAFE.compareAndSwapObject(this, waitersOffset, q.next = waiters, q); // 节点 q 入栈,做为新的头节点 waiters else if (timed) { nanos = deadline - System.nanoTime(); if (nanos <= 0L) { removeWaiter(q); // 已超时,将节点出栈 return state; } LockSupport.parkNanos(this, nanos); } else LockSupport.park(this); // 进入阻塞,由 FutureTask#run 或 FutureTask#cancel 来唤醒(内部均调用 FutureTask#finishCompletion) } }
当前线程等待完毕,获取任务的执行结果,或者抛出异常。
java.util.concurrent.FutureTask#report
/** * Returns result or throws exception for completed task. * * @param s completed state value */ @SuppressWarnings("unchecked") private V report(int s) throws ExecutionException { Object x = outcome; if (s == NORMAL) return (V)x; if (s >= CANCELLED) // CANCELLED、INTERRUPTING、INTERRUPTED throw new CancellationException(); throw new ExecutionException((Throwable)x); }
在必定的时间以内,等待获取任务执行的结果。
/** * @throws CancellationException {@inheritDoc} */ public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { if (unit == null) throw new NullPointerException(); int s = state; if (s <= COMPLETING && (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING) throw new TimeoutException(); // 等待超时了,任务尚未执行完,则抛出 TimeoutException return report(s); }
尝试取消任务的执行:
若是任务正在执行,方法的参数就会指示线程是否须要中断:
状态变化:
NEW -> CANCELLED
NEW -> INTERRUPTING -> INTERRUPTED
public boolean cancel(boolean mayInterruptIfRunning) { // 若是任务尚未启动(NEW),则修改任务状态(INTERRUPTING or CANCELLED),修改为功则进入下一步 // 若是任务状态不是 NEW,则直接返回。说明任务已完结(已完成、已取消、出现异常),没法取消,返回 false if (!(state == NEW && UNSAFE.compareAndSwapInt(this, stateOffset, NEW, mayInterruptIfRunning ? INTERRUPTING : CANCELLED))) return false; try { // in case call to interrupt throws exception // 进入这里,说明任务状态为 INTERRUPTING or CANCELLED // mayInterruptIfRunning 为 true 说明须要中断执行任务的线程,为 false 容许任务继续执行完 if (mayInterruptIfRunning) { try { Thread t = runner; if (t != null) t.interrupt(); } finally { // final state // 只有一个线程会执行到这里,无需使用 CAS UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); // INTERRUPTING -> INTERRUPTED } } } finally { finishCompletion(); // 唤醒等待线程 } return true; }
使用三个线程依次执行:提交任务、等待任务、取消任务。
观察执行结果,理解并发状况下多个线程之间如何使用 Future 进行交互。
/** * 三个线程依次执行:提交任务、等待任务、取消任务 * 在任务未执行完的时候,取消任务。 * * @author Sumkor * @since 2021/4/28 */ @Test public void cancel() throws InterruptedException { // 定义任务 FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(10000); return "哦豁"; } }); CountDownLatch submitGate = new CountDownLatch(1); // 等待任务提交 CountDownLatch endGate = new CountDownLatch(3); // 等待线程执行完 // 提交任务 new Thread(new Runnable() { @Override public void run() { try { submitGate.countDown(); System.out.println(Thread.currentThread().getName() + " 执行任务开始"); futureTask.run(); System.out.println(Thread.currentThread().getName() + " 执行任务结束"); } finally { endGate.countDown(); } } }).start(); // 等待任务 new Thread(new Runnable() { @Override public void run() { try { submitGate.await(); Thread.sleep(1000);// 等待 futureTask.run() 执行一段时间后再获取结果 System.out.println(Thread.currentThread().getName() + " 获取任务结果开始"); String result = futureTask.get(); System.out.println(Thread.currentThread().getName() + " 获取任务结果结束 " + result); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " 获取任务结果失败 " + e.getMessage()); e.printStackTrace(); } finally { endGate.countDown(); } } }).start(); // 取消任务 new Thread(new Runnable() { @Override public void run() { try { submitGate.await(); Thread.sleep(2000);// 等待 futureTask.get() 执行一段时间后再取消任务 System.out.println(Thread.currentThread().getName() + " 取消任务开始"); boolean cancel = futureTask.cancel(true); System.out.println(Thread.currentThread().getName() + " 取消任务结束 " + cancel); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " 取消任务失败 " + e.getMessage()); e.printStackTrace(); } finally { endGate.countDown(); } } }).start(); endGate.await(); }
执行结果:
Thread-0 执行任务开始 Thread-1 获取任务结果开始 Thread-2 取消任务开始 Thread-2 取消任务结束 true Thread-0 执行任务结束 Thread-1 获取任务结果失败 null java.util.concurrent.CancellationException at java.util.concurrent.FutureTask.report(FutureTask.java:121) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at com.sumkor.pool.FutureTest$6.run(FutureTest.java:129) at java.lang.Thread.run(Thread.java:745)
说明: