/** * A task that returns a result and may throw an exception. * Implementors define a single method with no arguments called * {@code call}. * * <p>The {@code Callable} interface is similar to {@link * java.lang.Runnable}, in that both are designed for classes whose * instances are potentially executed by another thread. A * {@code Runnable}, however, does not return a result and cannot * throw a checked exception. * * <p>The {@link Executors} class contains utility methods to * convert from other common forms to {@code Callable} classes. * * @see Executor * @since 1.5 * @author Doug Lea * @param <V> the result type of method {@code call} */@FunctionalInterfacepublicinterfaceCallable<V>{/** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */Vcall()throwsException;}
/** * A {@code Future} represents the result of an asynchronous * computation. Methods are provided to check if the computation is * complete, to wait for its completion, and to retrieve the result of * the computation. The result can only be retrieved using method * {@code get} when the computation has completed, blocking if * necessary until it is ready. Cancellation is performed by the * {@code cancel} method. Additional methods are provided to * determine if the task completed normally or was cancelled. Once a * computation has completed, the computation cannot be cancelled. * If you would like to use a {@code Future} for the sake * of cancellability but not provide a usable result, you can * declare types of the form {@code Future<?>} and * return {@code null} as a result of the underlying task. * * <p> * <b>Sample Usage</b> (Note that the following classes are all * made-up.) * * <pre> {@code * interface ArchiveSearcher { String search(String target); } * class App { * ExecutorService executor = ... * ArchiveSearcher searcher = ... * void showSearch(final String target) * throws InterruptedException { * Future<String> future * = executor.submit(new Callable<String>() { * public String call() { * return searcher.search(target); * }}); * displayOtherThings(); // do other things while searching * try { * displayText(future.get()); // use future * } catch (ExecutionException ex) { cleanup(); return; } * } * }}</pre> * * The {@link FutureTask} class is an implementation of {@code Future} that * implements {@code Runnable}, and so may be executed by an {@code Executor}. * For example, the above construction with {@code submit} could be replaced by: * <pre> {@code * FutureTask<String> future = * new FutureTask<>(new Callable<String>() { * public String call() { * return searcher.search(target); * }}); * executor.execute(future);}</pre> * * <p>Memory consistency effects: Actions taken by the asynchronous computation * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a> * actions following the corresponding {@code Future.get()} in another thread. * * @see FutureTask * @see Executor * @since 1.5 * @author Doug Lea * @param <V> The result type returned by this Future's {@code get} method */publicinterfaceFuture<V>{/** * Attempts to cancel execution of this task. This attempt will * fail if the task has already completed, has already been cancelled, * or could not be cancelled for some other reason. If successful, * and this task has not started when {@code cancel} is called, * this task should never run. If the task has already started, * then the {@code mayInterruptIfRunning} parameter determines * whether the thread executing this task should be interrupted in * an attempt to stop the task. * * <p>After this method returns, subsequent calls to {@link #isDone} will * always return {@code true}. Subsequent calls to {@link #isCancelled} * will always return {@code true} if this method returned {@code true}. * * @param mayInterruptIfRunning {@code true} if the thread executing this * task should be interrupted; otherwise, in-progress tasks are allowed * to complete * @return {@code false} if the task could not be cancelled, * typically because it has already completed normally; * {@code true} otherwise */booleancancel(booleanmayInterruptIfRunning);/** * Returns {@code true} if this task was cancelled before it completed * normally. * * @return {@code true} if this task was cancelled before it completed */booleanisCancelled();/** * Returns {@code true} if this task completed. * * Completion may be due to normal termination, an exception, or * cancellation -- in all of these cases, this method will return * {@code true}. * * @return {@code true} if this task completed */booleanisDone();/** * Waits if necessary for the computation to complete, and then * retrieves its result. * * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting */Vget()throwsInterruptedException,ExecutionException;/** * Waits if necessary for at most the given time for the computation * to complete, and then retrieves its result, if available. * * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting * @throws TimeoutException if the wait timed out */Vget(longtimeout,TimeUnitunit)throwsInterruptedException,ExecutionException,TimeoutException;}
/** * 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 */privatevolatileintstate;privatestaticfinalintNEW=0;privatestaticfinalintCOMPLETING=1;privatestaticfinalintNORMAL=2;privatestaticfinalintEXCEPTIONAL=3;privatestaticfinalintCANCELLED=4;privatestaticfinalintINTERRUPTING=5;privatestaticfinalintINTERRUPTED=6;
/** The underlying callable; nulled out after running */privateCallable<V>callable;/** The result to return or exception to throw from get() */privateObjectoutcome;// non-volatile, protected by state reads/writes/** The thread running the callable; CASed during run() */privatevolatileThreadrunner;/** Treiber stack of waiting threads */privatevolatileWaitNodewaiters;
publicvoidrun(){if(state!=NEW||!U.compareAndSwapObject(this,RUNNER,null,Thread.currentThread()))return;try{Callable<V>c=callable;if(c!=null&&state==NEW){Vresult;booleanran;try{result=c.call();ran=true;}catch(Throwableex){result=null;ran=false;setException(ex);// 任务执行抛出异常走setException}if(ran)set(result);// 任务正常执行完成走set}}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 interruptsints=state;if(s>=INTERRUPTING)handlePossibleCancellationInterrupt(s);}}
/** * 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 */protectedvoidset(Vv){if(U.compareAndSwapInt(this,STATE,NEW,COMPLETING)){outcome=v;U.putOrderedInt(this,STATE,NORMAL);// final statefinishCompletion();}}/** * Removes and signals all waiting threads, invokes done(), and * nulls out callable. */privatevoidfinishCompletion(){// assert state > COMPLETING;for(WaitNodeq;(q=waiters)!=null;){if(U.compareAndSwapObject(this,WAITERS,q,null)){for(;;){Threadt=q.thread;if(t!=null){q.thread=null;LockSupport.unpark(t);}WaitNodenext=q.next;if(next==null)break;q.next=null;// unlink to help gcq=next;}break;}}done();callable=null;// to reduce footprint}/** * 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 */protectedvoidsetException(Throwablet){if(U.compareAndSwapInt(this,STATE,NEW,COMPLETING)){outcome=t;U.putOrderedInt(this,STATE,EXCEPTIONAL);// final statefinishCompletion();}}
/** * @throws CancellationException {@inheritDoc} */publicVget()throwsInterruptedException,ExecutionException{ints=state;if(s<=COMPLETING)s=awaitDone(false,0L);returnreport(s);}/** * @throws CancellationException {@inheritDoc} */publicVget(longtimeout,TimeUnitunit)throwsInterruptedException,ExecutionException,TimeoutException{if(unit==null)thrownewNullPointerException();ints=state;if(s<=COMPLETING&&(s=awaitDone(true,unit.toNanos(timeout)))<=COMPLETING)thrownewTimeoutException();returnreport(s);}/** * 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 or at timeout */privateintawaitDone(booleantimed,longnanos)throwsInterruptedException{// The code below is very delicate, to achieve these goals:// - call nanoTime exactly once for each call to park// - if nanos <= 0L, return promptly without allocation or nanoTime// - if nanos == Long.MIN_VALUE, don't underflow// - if nanos == Long.MAX_VALUE, and nanoTime is non-monotonic// and we suffer a spurious wakeup, we will do no worse than// to park-spin for a whilelongstartTime=0L;// Special value 0L means not yet parkedWaitNodeq=null;booleanqueued=false;for(;;){ints=state;if(s>COMPLETING){if(q!=null)q.thread=null;returns;}elseif(s==COMPLETING)// We may have already promised (via isDone) that we are done// so never return empty-handed or throw InterruptedExceptionThread.yield();elseif(Thread.interrupted()){removeWaiter(q);thrownewInterruptedException();}elseif(q==null){if(timed&&nanos<=0L)returns;q=newWaitNode();}elseif(!queued)queued=U.compareAndSwapObject(this,WAITERS,q.next=waiters,q);elseif(timed){finallongparkNanos;if(startTime==0L){// first timestartTime=System.nanoTime();if(startTime==0L)startTime=1L;parkNanos=nanos;}else{longelapsed=System.nanoTime()-startTime;if(elapsed>=nanos){removeWaiter(q);returnstate;}parkNanos=nanos-elapsed;}// nanoTime may be slow; recheck before parkingif(state<COMPLETING)LockSupport.parkNanos(this,parkNanos);}elseLockSupport.park(this);}/** * Returns result or throws exception for completed task. * * @param s completed state value */@SuppressWarnings("unchecked")privateVreport(ints)throwsExecutionException{Objectx=outcome;if(s==NORMAL)return(V)x;if(s>=CANCELLED)thrownewCancellationException();thrownewExecutionException((Throwable)x);}
/** * Simple linked list nodes to record waiting threads in a Treiber * stack. See other classes such as Phaser and SynchronousQueue * for more detailed explanation. */staticfinalclassWaitNode{volatileThreadthread;volatileWaitNodenext;WaitNode(){thread=Thread.currentThread();}}
publicbooleancancel(booleanmayInterruptIfRunning){if(!(state==NEW&&U.compareAndSwapInt(this,STATE,NEW,mayInterruptIfRunning?INTERRUPTING:CANCELLED)))returnfalse;try{// in case call to interrupt throws exceptionif(mayInterruptIfRunning){try{Threadt=runner;if(t!=null)t.interrupt();}finally{// final stateU.putOrderedInt(this,STATE,INTERRUPTED);}}}finally{finishCompletion();}returntrue;}
/** * Executes the computation without setting its result, and then * resets this future to initial state, failing to do so if the * computation encounters an exception or is cancelled. This is * designed for use with tasks that intrinsically execute more * than once. * * @return {@code true} if successfully run and reset */protectedbooleanrunAndReset(){if(state!=NEW||!U.compareAndSwapObject(this,RUNNER,null,Thread.currentThread()))returnfalse;booleanran=false;ints=state;try{Callable<V>c=callable;if(c!=null&&s==NEW){try{c.call();// don't set resultran=true;}catch(Throwableex){setException(ex);}}}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 interruptss=state;if(s>=INTERRUPTING)handlePossibleCancellationInterrupt(s);}returnran&&s==NEW;}