< prev index next >

src/java.base/share/classes/java/util/concurrent/FutureTask.java

Print this page

188         int s = state;
189         if (s <= COMPLETING)
190             s = awaitDone(false, 0L);
191         return report(s);
192     }
193 
194     /**
195      * @throws CancellationException {@inheritDoc}
196      */
197     public V get(long timeout, TimeUnit unit)
198         throws InterruptedException, ExecutionException, TimeoutException {
199         if (unit == null)
200             throw new NullPointerException();
201         int s = state;
202         if (s <= COMPLETING &&
203             (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
204             throw new TimeoutException();
205         return report(s);
206     }
207 










































208     /**
209      * Protected method invoked when this task transitions to state
210      * {@code isDone} (whether normally or via cancellation). The
211      * default implementation does nothing.  Subclasses may override
212      * this method to invoke completion callbacks or perform
213      * bookkeeping. Note that you can query status inside the
214      * implementation of this method to determine whether this task
215      * has been cancelled.
216      */
217     protected void done() { }
218 
219     /**
220      * Sets the result of this future to the given value unless
221      * this future has already been set or has been cancelled.
222      *
223      * <p>This method is invoked internally by the {@link #run} method
224      * upon successful completion of the computation.
225      *
226      * @param v the value
227      */

188         int s = state;
189         if (s <= COMPLETING)
190             s = awaitDone(false, 0L);
191         return report(s);
192     }
193 
194     /**
195      * @throws CancellationException {@inheritDoc}
196      */
197     public V get(long timeout, TimeUnit unit)
198         throws InterruptedException, ExecutionException, TimeoutException {
199         if (unit == null)
200             throw new NullPointerException();
201         int s = state;
202         if (s <= COMPLETING &&
203             (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
204             throw new TimeoutException();
205         return report(s);
206     }
207 
208     @Override
209     public V completedResultNow() {
210         if (state == NORMAL) {
211             @SuppressWarnings("unchecked")
212             V result = (V) outcome;
213             return result;
214         } else {
215             throw new IllegalStateException();
216         }
217     }
218 
219     @Override
220     public Throwable completedExceptionNow() {
221         switch (state) {
222             case EXCEPTIONAL:
223                 Object x = outcome;
224                 return (Throwable) x;
225             case CANCELLED:
226             case INTERRUPTING:
227             case INTERRUPTED:
228                 return new CancellationException();
229             default:
230                 throw new IllegalStateException();
231         }
232     }
233 
234     @Override
235     public State state() {
236         switch (state) {
237             case NORMAL:
238                 return State.SUCCESS;
239             case EXCEPTIONAL:
240                 return State.FAILED;
241             case CANCELLED:
242             case INTERRUPTING:
243             case INTERRUPTED:
244                 return State.CANCELLED;
245             default:
246                 return State.RUNNING;
247         }
248     }
249 
250     /**
251      * Protected method invoked when this task transitions to state
252      * {@code isDone} (whether normally or via cancellation). The
253      * default implementation does nothing.  Subclasses may override
254      * this method to invoke completion callbacks or perform
255      * bookkeeping. Note that you can query status inside the
256      * implementation of this method to determine whether this task
257      * has been cancelled.
258      */
259     protected void done() { }
260 
261     /**
262      * Sets the result of this future to the given value unless
263      * this future has already been set or has been cancelled.
264      *
265      * <p>This method is invoked internally by the {@link #run} method
266      * upon successful completion of the computation.
267      *
268      * @param v the value
269      */
< prev index next >