jdk/src/share/classes/java/util/concurrent/FutureTask.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7518 0282db800fe1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A cancellable asynchronous computation.  This class provides a base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * implementation of {@link Future}, with methods to start and cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a computation, query to see if the computation is complete, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * retrieve the result of the computation.  The result can only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * retrieved when the computation has completed; the <tt>get</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * method will block if the computation has not yet completed.  Once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the computation has completed, the computation cannot be restarted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * or cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>A <tt>FutureTask</tt> can be used to wrap a {@link Callable} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * {@link java.lang.Runnable} object.  Because <tt>FutureTask</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * implements <tt>Runnable</tt>, a <tt>FutureTask</tt> can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * submitted to an {@link Executor} for execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>In addition to serving as a standalone class, this class provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <tt>protected</tt> functionality that may be useful when creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * customized task classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @param <V> The result type returned by this FutureTask's <tt>get</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class FutureTask<V> implements RunnableFuture<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** Synchronization control for FutureTask */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final Sync sync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a <tt>FutureTask</tt> that will, upon running, execute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * given <tt>Callable</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param  callable the callable task
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @throws NullPointerException if callable is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public FutureTask(Callable<V> callable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (callable == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        sync = new Sync(callable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates a <tt>FutureTask</tt> that will, upon running, execute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * given <tt>Runnable</tt>, and arrange that <tt>get</tt> will return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * given result on successful completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param runnable the runnable task
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param result the result to return on successful completion. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * you don't need a particular result, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * constructions of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <tt>Future&lt;?&gt; f = new FutureTask&lt;Object&gt;(runnable, null)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws NullPointerException if runnable is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public FutureTask(Runnable runnable, V result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        sync = new Sync(Executors.callable(runnable, result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public boolean isCancelled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return sync.innerIsCancelled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public boolean isDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return sync.innerIsDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public boolean cancel(boolean mayInterruptIfRunning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return sync.innerCancel(mayInterruptIfRunning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws CancellationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public V get() throws InterruptedException, ExecutionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return sync.innerGet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws CancellationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public V get(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return sync.innerGet(unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Protected method invoked when this task transitions to state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <tt>isDone</tt> (whether normally or via cancellation). The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * default implementation does nothing.  Subclasses may override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * this method to invoke completion callbacks or perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * bookkeeping. Note that you can query status inside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * implementation of this method to determine whether this task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected void done() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Sets the result of this Future to the given value unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * this future has already been set or has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * This method is invoked internally by the <tt>run</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * upon successful completion of the computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param v the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected void set(V v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        sync.innerSet(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Causes this future to report an <tt>ExecutionException</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * with the given throwable as its cause, unless this Future has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * already been set or has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * This method is invoked internally by the <tt>run</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * upon failure of the computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param t the cause of failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    protected void setException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        sync.innerSetException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // The following (duplicated) doc comment can be removed once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // 6270645: Javadoc comments should be inherited from most derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    //          superinterface or superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Sets this Future to the result of its computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * unless it has been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        sync.innerRun();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Executes the computation without setting its result, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * resets this Future to initial state, failing to do so if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * computation encounters an exception or is cancelled.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * designed for use with tasks that intrinsically execute more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return true if successfully run and reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected boolean runAndReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return sync.innerRunAndReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Synchronization control for FutureTask. Note that this must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * a non-static inner class in order to invoke the protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <tt>done</tt> method. For clarity, all inner class support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * methods are same as outer, prefixed with "inner".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Uses AQS sync state to represent run status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private final class Sync extends AbstractQueuedSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        private static final long serialVersionUID = -7828117401763700385L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /** State value representing that task is ready to run */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        private static final int READY     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /** State value representing that task is running */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        private static final int RUNNING   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /** State value representing that task ran */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        private static final int RAN       = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        /** State value representing that task was cancelled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        private static final int CANCELLED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /** The underlying callable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private final Callable<V> callable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /** The result to return from get() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        private V result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /** The exception to throw from get() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        private Throwable exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * The thread running task. When nulled after set/cancel, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * indicates that the results are accessible.  Must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * volatile, to ensure visibility upon completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        private volatile Thread runner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Sync(Callable<V> callable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            this.callable = callable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        private boolean ranOrCancelled(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return (state & (RAN | CANCELLED)) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * Implements AQS base acquire to succeed if ran or cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        protected int tryAcquireShared(int ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return innerIsDone() ? 1 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * Implements AQS base release to always signal after setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * final done status by nulling runner thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        protected boolean tryReleaseShared(int ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            runner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        boolean innerIsCancelled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return getState() == CANCELLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        boolean innerIsDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return ranOrCancelled(getState()) && runner == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        V innerGet() throws InterruptedException, ExecutionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            acquireSharedInterruptibly(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (getState() == CANCELLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                throw new CancellationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (exception != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw new ExecutionException(exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        V innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (!tryAcquireSharedNanos(0, nanosTimeout))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw new TimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (getState() == CANCELLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                throw new CancellationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (exception != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                throw new ExecutionException(exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        void innerSet(V v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                int s = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (s == RAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (s == CANCELLED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    // aggressively release to set runner to null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    // in case we are racing with a cancel request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    // that will try to interrupt runner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    releaseShared(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (compareAndSetState(s, RAN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    result = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    releaseShared(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        void innerSetException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                int s = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (s == RAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (s == CANCELLED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    // aggressively release to set runner to null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    // in case we are racing with a cancel request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    // that will try to interrupt runner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    releaseShared(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                if (compareAndSetState(s, RAN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    exception = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    releaseShared(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        boolean innerCancel(boolean mayInterruptIfRunning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                int s = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                if (ranOrCancelled(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (compareAndSetState(s, CANCELLED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (mayInterruptIfRunning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                Thread r = runner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (r != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    r.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            releaseShared(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        void innerRun() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (!compareAndSetState(READY, RUNNING))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            runner = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (getState() == RUNNING) { // recheck after setting thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                V result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    result = callable.call();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    setException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                set(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                releaseShared(0); // cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        boolean innerRunAndReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (!compareAndSetState(READY, RUNNING))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                runner = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (getState() == RUNNING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    callable.call(); // don't set result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                runner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                return compareAndSetState(RUNNING, READY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                setException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}