< prev index next >

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

Print this page

144      * while waiting
145      */
146     V get() throws InterruptedException, ExecutionException;
147 
148     /**
149      * Waits if necessary for at most the given time for the computation
150      * to complete, and then retrieves its result, if available.
151      *
152      * @param timeout the maximum time to wait
153      * @param unit the time unit of the timeout argument
154      * @return the computed result
155      * @throws CancellationException if the computation was cancelled
156      * @throws ExecutionException if the computation threw an
157      * exception
158      * @throws InterruptedException if the current thread was interrupted
159      * while waiting
160      * @throws TimeoutException if the wait timed out
161      */
162     V get(long timeout, TimeUnit unit)
163         throws InterruptedException, ExecutionException, TimeoutException;




























































































































164 }

144      * while waiting
145      */
146     V get() throws InterruptedException, ExecutionException;
147 
148     /**
149      * Waits if necessary for at most the given time for the computation
150      * to complete, and then retrieves its result, if available.
151      *
152      * @param timeout the maximum time to wait
153      * @param unit the time unit of the timeout argument
154      * @return the computed result
155      * @throws CancellationException if the computation was cancelled
156      * @throws ExecutionException if the computation threw an
157      * exception
158      * @throws InterruptedException if the current thread was interrupted
159      * while waiting
160      * @throws TimeoutException if the wait timed out
161      */
162     V get(long timeout, TimeUnit unit)
163         throws InterruptedException, ExecutionException, TimeoutException;
164 
165     /**
166      * Returns the computed result, without waiting.
167      *
168      * @implSpec
169      * The default implementation invokes {@code isDone()} to test if the task
170      * has completed. If done, it invokes {@code get()} to obtain the result.
171      *
172      * @return the computed result
173      * @throws IllegalStateException if the task has not completed or the task
174      * did not complete with a result
175      * @since 99
176      */
177     default V completedResultNow() {
178         if (!isDone())
179             throw new IllegalStateException();
180         boolean interrupted = false;
181         try {
182             while (true) {
183                 try {
184                     return get();
185                 } catch (InterruptedException e) {
186                     interrupted = true;
187                 } catch (ExecutionException | CancellationException e) {
188                     throw new IllegalStateException();
189                 }
190             }
191         } finally {
192             if (interrupted) Thread.currentThread().interrupt();
193         }
194     }
195 
196     /**
197      * Returns the exception thrown by the task or a CancellationException
198      * if cancelled, without waiting.
199      *
200      * @implSpec
201      * The default implementation invokes {@code isDone()} to test if the task
202      * has completed. If done and not cancelled, it invokes {@code get()} and
203      * catches the {@code ExecutionException} to obtain the exception.
204      *
205      * @return the exception thrown by the task
206      * @throws IllegalStateException if the task has not completed or the task
207      * completed normally
208      * @since 99
209      */
210     default Throwable completedExceptionNow() {
211         if (!isDone())
212             throw new IllegalStateException();
213         if (isCancelled())
214             return new CancellationException();
215         boolean interrupted = false;
216         try {
217             while (true) {
218                 try {
219                     get();
220                     throw new IllegalStateException();
221                 } catch (InterruptedException e) {
222                     interrupted = true;
223                 } catch (ExecutionException e) {
224                     return e.getCause();
225                 }
226             }
227         } finally {
228             if (interrupted) Thread.currentThread().interrupt();
229         }
230     }
231 
232     /**
233      * Represents the computation state.
234      * @since 99
235      */
236     enum State {
237         /**
238          * The task has not completed.
239          */
240         RUNNING,
241         /**
242          * The task completed with a result.
243          * @see Future#completedResultNow()
244          */
245         SUCCESS,
246         /**
247          * The task completed with an exception.
248          * @see Future#completedExceptionNow()
249          */
250         FAILED,
251         /**
252          * The task is cancelled.
253          * @see #cancel(boolean)
254          */
255         CANCELLED
256     }
257 
258     /**
259      * {@return the computation state}
260      *
261      * @implSpec
262      * The default implementation uses {@code isDone()}, {@code isCancelled()},
263      * and {@code get()} to determine the state.
264      *
265      * @since 99
266      */
267     default State state() {
268         if (!isDone())
269             return State.RUNNING;
270         if (isCancelled())
271             return State.CANCELLED;
272         boolean interrupted = false;
273         try {
274             while (true) {
275                 try {
276                     get();  // may throw InterruptedException when done
277                     return State.SUCCESS;
278                 } catch (InterruptedException e) {
279                     interrupted = true;
280                 } catch (ExecutionException e) {
281                     return State.FAILED;
282                 }
283             }
284         } finally {
285             if (interrupted) Thread.currentThread().interrupt();
286         }
287     }
288 }
< prev index next >