0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * Written by Doug Lea with assistance from members of JCP JSR-166 0N/A * Expert Group and released to the public domain, as explained at 0N/A * A cancellable asynchronous computation. This class provides a base 0N/A * implementation of {@link Future}, with methods to start and cancel 0N/A * a computation, query to see if the computation is complete, and 0N/A * retrieve the result of the computation. The result can only be 6144N/A * retrieved when the computation has completed; the {@code get} 6144N/A * methods will block if the computation has not yet completed. Once 0N/A * the computation has completed, the computation cannot be restarted 6144N/A * or cancelled (unless the computation is invoked using 6144N/A * <p>A {@code FutureTask} can be used to wrap a {@link Callable} or 6144N/A * {@link Runnable} object. Because {@code FutureTask} implements 6144N/A * {@code Runnable}, a {@code FutureTask} can be submitted to an 6144N/A * {@link Executor} for execution. 0N/A * <p>In addition to serving as a standalone class, this class provides 6144N/A * {@code protected} functionality that may be useful when creating 0N/A * customized task classes. 6144N/A * @param <V> The result type returned by this FutureTask's {@code get} methods 6144N/A * Revision notes: This differs from previous versions of this 6144N/A * class that relied on AbstractQueuedSynchronizer, mainly to 6144N/A * avoid surprising users about retaining interrupt status during 6144N/A * cancellation races. Sync control in the current design relies 6144N/A * on a "state" field updated via CAS to track completion, along 6144N/A * with a simple Treiber stack to hold waiting threads. 6144N/A * Style note: As usual, we bypass overhead of using 6144N/A * AtomicXFieldUpdaters and instead directly use Unsafe intrinsics. 6144N/A * The run state of this task, initially NEW. The run state 6144N/A * transitions to a terminal state only in methods set, 6144N/A * setException, and cancel. During completion, state may take on 6144N/A * transient values of COMPLETING (while outcome is being set) or 6144N/A * INTERRUPTING (only while interrupting the runner to satisfy a 6144N/A * cancel(true)). Transitions from these intermediate to final 6144N/A * and cannot be further modified. 6144N/A * Possible state transitions: 6144N/A * NEW -> COMPLETING -> NORMAL 6144N/A * NEW -> COMPLETING -> EXCEPTIONAL 6144N/A * NEW -> INTERRUPTING -> INTERRUPTED 6144N/A /** The underlying callable; nulled out after running */ 6144N/A /** The result to return or exception to throw from get() */ 6144N/A /** The thread running the callable; CASed during run() */ 6144N/A /** Treiber stack of waiting threads */ 6144N/A * Returns result or throws exception for completed task. 6144N/A * @param s completed state value 6144N/A * Creates a {@code FutureTask} that will, upon running, execute the 0N/A * @param callable the callable task 6144N/A * @throws NullPointerException if the callable is null 6144N/A * Creates a {@code FutureTask} that will, upon running, execute the 6144N/A * given {@code Runnable}, and arrange that {@code get} will return the 0N/A * given result on successful completion. 0N/A * @param runnable the runnable task 0N/A * @param result the result to return on successful completion. If 0N/A * you don't need a particular result, consider using 0N/A * constructions of the form: 3203N/A * {@code Future<?> f = new FutureTask<Void>(runnable, null)} 6144N/A * @throws NullPointerException if the runnable is null 0N/A * @throws CancellationException {@inheritDoc} 0N/A * @throws CancellationException {@inheritDoc} 0N/A * Protected method invoked when this task transitions to state 6144N/A * {@code isDone} (whether normally or via cancellation). The 0N/A * default implementation does nothing. Subclasses may override 0N/A * this method to invoke completion callbacks or perform 0N/A * bookkeeping. Note that you can query status inside the 0N/A * implementation of this method to determine whether this task 0N/A * has been cancelled. 6144N/A * Sets the result of this future to the given value unless 0N/A * this future has already been set or has been cancelled. 6144N/A * <p>This method is invoked internally by the {@link #run} method 0N/A * upon successful completion of the computation. 0N/A * @param v the value 6144N/A * Causes this future to report an {@link ExecutionException} 6144N/A * with the given throwable as its cause, unless this future has 0N/A * already been set or has been cancelled. 6144N/A * <p>This method is invoked internally by the {@link #run} method 0N/A * upon failure of the computation. 0N/A * @param t the cause of failure 6144N/A // runner must be non-null until state is settled to 6144N/A // prevent concurrent calls to run() 6144N/A // state must be re-read after nulling runner to prevent 0N/A * Executes the computation without setting its result, and then 6144N/A * resets this future to initial state, failing to do so if the 0N/A * computation encounters an exception or is cancelled. This is 0N/A * designed for use with tasks that intrinsically execute more 0N/A * @return true if successfully run and reset 6144N/A // runner must be non-null until state is settled to 6144N/A // prevent concurrent calls to run() 6144N/A // state must be re-read after nulling runner to prevent 6144N/A * Ensures that any interrupt from a possible cancel(true) is only 6144N/A * delivered to a task while in run or runAndReset. 6144N/A // It is possible for our interrupter to stall before getting a 6144N/A // chance to interrupt us. Let's spin-wait patiently. 6144N/A // assert state == INTERRUPTED; 6144N/A // We want to clear any interrupt we may have received from 6144N/A // cancel(true). However, it is permissible to use interrupts 6144N/A // as an independent mechanism for a task to communicate with 6144N/A // its caller, and there is no way to clear only the 6144N/A * Simple linked list nodes to record waiting threads in a Treiber 6144N/A * stack. See other classes such as Phaser and SynchronousQueue 6144N/A * for more detailed explanation. 6144N/A * Removes and signals all waiting threads, invokes done(), and 6144N/A // assert state > COMPLETING; 6144N/A * Awaits completion or aborts on interrupt or timeout. 6144N/A * @param timed true if use timed waits 6144N/A * @param nanos time to wait, if timed 6144N/A * @return state upon completion 6144N/A * Tries to unlink a timed-out or interrupted wait node to avoid 6144N/A * accumulating garbage. Internal nodes are simply unspliced 6144N/A * without CAS since it is harmless if they are traversed anyway 6144N/A * by releasers. To avoid effects of unsplicing from already 6144N/A * removed nodes, the list is retraversed in case of an apparent 6144N/A * race. This is slow when there are a lot of nodes, but we don't 6144N/A * expect lists to be long enough to outweigh higher-overhead 6144N/A for (;;) {
// restart on removeWaiter race