[concurrency-interest] Future.isCompletedNormally Future.isCompletedAbnormally

Vitaly Davidovich vitalyd at gmail.com
Thu Mar 5 00:23:31 EST 2015


What's the motivation for doing that? Why moving this up in the hierarchy
is desired? These methods seem like utilities one would write on top of
their own concrete implementations.

Also, what's the purpose of the labeled loop? Moreover, what if get () on
that impl always throws interrupted exception - this loops forever? I guess
I don't like this because it makes assumptions about how the implementation
behaves in get ().

sent from my phone
On Mar 4, 2015 10:48 PM, "Martin Buchholz" <martinrb at google.com> wrote:

> ForkJoinTask is a Future implementation with existing methods
> isCompletedNormally isCompletedAbnormally.
>
> We could make these methods on Future itself with (inefficient) default
> implementations and efficient implementations on FutureTask and
> CompletableFuture.
>
> Worth doing?
>
> Here's a v0.1:
>
> Index: CompletableFuture.java
> ===================================================================
> RCS file:
> /export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/CompletableFuture.java,v
> retrieving revision 1.158
> diff -u -r1.158 CompletableFuture.java
> --- CompletableFuture.java    17 Jan 2015 20:05:02 -0000    1.158
> +++ CompletableFuture.java    5 Mar 2015 03:18:19 -0000
> @@ -2314,6 +2314,32 @@
>      }
>
>      /**
> +     * Returns {@code true} if this CompletableFuture exceptionally and
> was not cancelled.
> +     *
> +     * @return {@code true} if this CompletableFuture completed
> exceptionally and was not cancelled
> +     */
> +    public final boolean isCompletedAbnormally() {
> +        Object r;
> +        return ((r = result) instanceof AltResult)
> +            && r != NIL
> +            && !(((AltResult)r).ex instanceof CancellationException);
> +    }
> +
> +    /**
> +     * Returns {@code true} if this task completed without throwing an
> +     * exception and was not cancelled.
> +     *
> +     * @return {@code true} if this task completed without throwing an
> +     * exception and was not cancelled
> +     */
> +    public final boolean isCompletedNormally() {
> +        Object r;
> +        return (r = result) != null
> +            && (r == NIL
> +                || !(r instanceof AltResult));
> +    }
> +
> +    /**
>       * Forcibly sets or resets the value subsequently returned by
>       * method {@link #get()} and related methods, whether or not
>       * already completed. This method is designed for use only in
> Index: Future.java
> ===================================================================
> RCS file:
> /export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/Future.java,v
> retrieving revision 1.40
> diff -u -r1.40 Future.java
> --- Future.java    17 Feb 2015 18:55:39 -0000    1.40
> +++ Future.java    5 Mar 2015 03:18:19 -0000
> @@ -110,6 +110,66 @@
>      boolean isDone();
>
>      /**
> +     * Returns {@code true} if this task completed by throwing an
> exception.
> +     *
> +     * @return {@code true} if this task completed by throwing an
> exception
> +     */
> +    default boolean isCompletedAbnormally() {
> +        if (!isDone() || isCancelled())
> +            return false;
> +        // Support Future implementations that throw
> +        // InterruptedException even when result is available.
> +        boolean interrupted = false;
> +        try {
> +            retry: for (;;) {
> +                try {
> +                    get();
> +                } catch (ExecutionException e) {
> +                    return true;
> +                } catch (InterruptedException e) {
> +                    interrupted = true;
> +                    continue retry;
> +                }
> +                return false;
> +            }
> +        } finally {
> +            if (interrupted)
> +                Thread.currentThread().interrupt();
> +        }
> +    }
> +
> +    /**
> +     * Returns {@code true} if this task completed without throwing an
> +     * exception and was not cancelled.
> +     *
> +     * @return {@code true} if this task completed without throwing an
> +     * exception and was not cancelled
> +     */
> +    default boolean isCompletedNormally() {
> +        if (!isDone() || isCancelled())
> +            return false;
> +        // Support Future implementations that throw
> +        // InterruptedException even when result is available.
> +        boolean interrupted = false;
> +        try {
> +            retry: for (;;) {
> +                try {
> +                    get();
> +                    return true;
> +                } catch (ExecutionException e) {
> +                } catch (InterruptedException e) {
> +                    interrupted = true;
> +                    continue retry;
> +                }
> +                return false;
> +            }
> +        } finally {
> +            if (interrupted)
> +                Thread.currentThread().interrupt();
> +        }
> +    }
> +
> +    /**
>       * Waits if necessary for the computation to complete, and then
>       * retrieves its result.
>       *
> Index: FutureTask.java
> ===================================================================
> RCS file:
> /export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/FutureTask.java,v
> retrieving revision 1.111
> diff -u -r1.111 FutureTask.java
> --- FutureTask.java    5 Mar 2015 00:32:52 -0000    1.111
> +++ FutureTask.java    5 Mar 2015 03:18:19 -0000
> @@ -133,6 +133,32 @@
>          return state != NEW;
>      }
>
> +    /**
> +     * Returns {@code true} if this task completed by throwing an
> exception.
> +     *
> +     * @return {@code true} if this task completed by throwing an
> exception
> +     */
> +    public final boolean isCompletedAbnormally() {
> +        int s;
> +        while ((s = state) == COMPLETING)
> +            Thread.yield();
> +        return s == EXCEPTIONAL;
> +    }
> +
> +    /**
> +     * Returns {@code true} if this task completed without throwing an
> +     * exception and was not cancelled.
> +     *
> +     * @return {@code true} if this task completed without throwing an
> +     * exception and was not cancelled
> +     */
> +    public final boolean isCompletedNormally() {
> +        int s;
> +        while ((s = state) == COMPLETING)
> +            Thread.yield();
> +        return s == NORMAL;
> +    }
> +
>      public boolean cancel(boolean mayInterruptIfRunning) {
>          if (!(state == NEW &&
>                U.compareAndSwapInt(this, STATE, NEW,
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150305/ef258fe0/attachment.html>


More information about the Concurrency-interest mailing list