jdk/src/java.base/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java
author dl
Wed, 21 Dec 2016 14:26:52 -0800
changeset 42927 1d31e540bfcb
parent 34348 ba5c2f2fc9d7
child 45936 d564ef4bed8f
permissions -rw-r--r--
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12 Reviewed-by: martin, smarks, psandoz
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: 4110
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: 4110
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: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
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
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7976
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
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;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    37
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    38
import static java.util.concurrent.TimeUnit.MILLISECONDS;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
    39
import static java.util.concurrent.TimeUnit.NANOSECONDS;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    40
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    41
import java.util.AbstractQueue;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    42
import java.util.Arrays;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    43
import java.util.Collection;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    44
import java.util.Iterator;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    45
import java.util.List;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    46
import java.util.NoSuchElementException;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
    47
import java.util.concurrent.atomic.AtomicLong;
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
    48
import java.util.concurrent.locks.Condition;
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
    49
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * A {@link ThreadPoolExecutor} that can additionally schedule
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    53
 * commands to run after a given delay, or to execute periodically.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    54
 * This class is preferable to {@link java.util.Timer} when multiple
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    55
 * worker threads are needed, or when the additional flexibility or
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    56
 * capabilities of {@link ThreadPoolExecutor} (which this class
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    57
 * extends) are required.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
    59
 * <p>Delayed tasks execute no sooner than they are enabled, but
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * without any real-time guarantees about when, after they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * enabled, they will commence. Tasks scheduled for exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * execution time are enabled in first-in-first-out (FIFO) order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * submission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
    65
 * <p>When a submitted task is cancelled before it is run, execution
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    66
 * is suppressed.  By default, such a cancelled task is not
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    67
 * automatically removed from the work queue until its delay elapses.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    68
 * While this enables further inspection and monitoring, it may also
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    69
 * cause unbounded retention of cancelled tasks.  To avoid this, use
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    70
 * {@link #setRemoveOnCancelPolicy} to cause tasks to be immediately
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    71
 * removed from the work queue at time of cancellation.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
    72
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    73
 * <p>Successive executions of a periodic task scheduled via
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    74
 * {@link #scheduleAtFixedRate scheduleAtFixedRate} or
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    75
 * {@link #scheduleWithFixedDelay scheduleWithFixedDelay}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    76
 * do not overlap. While different executions may be performed by
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    77
 * different threads, the effects of prior executions
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    78
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1007
diff changeset
    79
 * those of subsequent ones.
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 1007
diff changeset
    80
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>While this class inherits from {@link ThreadPoolExecutor}, a few
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * of the inherited tuning methods are not useful for it. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * particular, because it acts as a fixed-sized pool using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * {@code corePoolSize} threads and an unbounded queue, adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * to {@code maximumPoolSize} have no useful effect. Additionally, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * is almost never a good idea to set {@code corePoolSize} to zero or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * use {@code allowCoreThreadTimeOut} because this may leave the pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * without threads to handle tasks once they become eligible to run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p><b>Extension notes:</b> This class overrides the
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    91
 * {@link ThreadPoolExecutor#execute(Runnable) execute} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * {@link AbstractExecutorService#submit(Runnable) submit}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * methods to generate internal {@link ScheduledFuture} objects to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * control per-task delays and scheduling.  To preserve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * functionality, any further overrides of these methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * subclasses must invoke superclass versions, which effectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * disables additional task customization.  However, this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * provides alternative protected extension method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * {@code decorateTask} (one version each for {@code Runnable} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * {@code Callable}) that can be used to customize the concrete task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * types used to execute commands entered via {@code execute},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * {@code submit}, {@code schedule}, {@code scheduleAtFixedRate},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * and {@code scheduleWithFixedDelay}.  By default, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * {@code ScheduledThreadPoolExecutor} uses a task type extending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * {@link FutureTask}. However, this may be modified or replaced using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * subclasses of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   108
 * <pre> {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   static class CustomTask<V> implements RunnableScheduledFuture<V> { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *   protected <V> RunnableScheduledFuture<V> decorateTask(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *                Runnable r, RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *       return new CustomTask<V>(r, task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *   protected <V> RunnableScheduledFuture<V> decorateTask(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *                Callable<V> c, RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *       return new CustomTask<V>(c, task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *   // ... add constructors, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
public class ScheduledThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        extends ThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        implements ScheduledExecutorService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This class specializes ThreadPoolExecutor implementation by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   135
     * 1. Using a custom task type ScheduledFutureTask, even for tasks
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   136
     *    that don't require scheduling because they are submitted
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   137
     *    using ExecutorService rather than ScheduledExecutorService
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   138
     *    methods, which are treated as tasks with a delay of zero.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   140
     * 2. Using a custom queue (DelayedWorkQueue), a variant of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *    unbounded DelayQueue. The lack of capacity constraint and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *    the fact that corePoolSize and maximumPoolSize are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *    effectively identical simplifies some execution mechanics
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   144
     *    (see delayedExecute) compared to ThreadPoolExecutor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * 3. Supporting optional run-after-shutdown parameters, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *    leads to overrides of shutdown methods to remove and cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *    tasks that should NOT be run after shutdown, as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *    different recheck logic when task (re)submission overlaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *    with a shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * 4. Task decoration methods to allow interception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *    instrumentation, which are needed because subclasses cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *    otherwise override submit methods to get this effect. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *    don't have any impact on pool control logic though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * False if should cancel/suppress periodic tasks on shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private volatile boolean continueExistingPeriodicTasksAfterShutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * False if should cancel non-periodic tasks on shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private volatile boolean executeExistingDelayedTasksAfterShutdown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   169
     * True if ScheduledFutureTask.cancel should remove from queue.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   170
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   171
    volatile boolean removeOnCancel;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   172
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   173
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Sequence number to break scheduling ties, and in turn to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * guarantee FIFO order among tied entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   177
    private static final AtomicLong sequencer = new AtomicLong();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private class ScheduledFutureTask<V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            extends FutureTask<V> implements RunnableScheduledFuture<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        /** Sequence number to break ties FIFO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        private final long sequenceNumber;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   184
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   185
        /** The nanoTime-based time when the task is enabled to execute. */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   186
        private volatile long time;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   187
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        /**
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   189
         * Period for repeating tasks, in nanoseconds.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   190
         * A positive value indicates fixed-rate execution.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   191
         * A negative value indicates fixed-delay execution.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   192
         * A value of 0 indicates a non-repeating (one-shot) task.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        private final long period;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /** The actual task to be re-enqueued by reExecutePeriodic */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        RunnableScheduledFuture<V> outerTask = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /**
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   200
         * Index into delay queue, to support faster cancellation.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   201
         */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   202
        int heapIndex;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   203
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   204
        /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * Creates a one-shot action with given nanoTime-based trigger time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   207
        ScheduledFutureTask(Runnable r, V result, long triggerTime,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   208
                            long sequenceNumber) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            super(r, result);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   210
            this.time = triggerTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            this.period = 0;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   212
            this.sequenceNumber = sequenceNumber;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   216
         * Creates a periodic action with given nanoTime-based initial
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   217
         * trigger time and period.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   219
        ScheduledFutureTask(Runnable r, V result, long triggerTime,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   220
                            long period, long sequenceNumber) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            super(r, result);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   222
            this.time = triggerTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            this.period = period;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   224
            this.sequenceNumber = sequenceNumber;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   228
         * Creates a one-shot action with given nanoTime-based trigger time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   230
        ScheduledFutureTask(Callable<V> callable, long triggerTime,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   231
                            long sequenceNumber) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            super(callable);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   233
            this.time = triggerTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.period = 0;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   235
            this.sequenceNumber = sequenceNumber;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public long getDelay(TimeUnit unit) {
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   239
            return unit.convert(time - System.nanoTime(), NANOSECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        public int compareTo(Delayed other) {
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   243
            if (other == this) // compare zero if same object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (other instanceof ScheduledFutureTask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                ScheduledFutureTask<?> x = (ScheduledFutureTask<?>)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                long diff = time - x.time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (diff < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                else if (diff > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                else if (sequenceNumber < x.sequenceNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   257
            long diff = getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS);
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   258
            return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   262
         * Returns {@code true} if this is a periodic (not a one-shot) action.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   264
         * @return {@code true} if periodic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        public boolean isPeriodic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return period != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * Sets the next time to run for a periodic task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        private void setNextRunTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            long p = period;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (p > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                time += p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            else
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   278
                time = triggerTime(-p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   281
        public boolean cancel(boolean mayInterruptIfRunning) {
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   282
            // The racy read of heapIndex below is benign:
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   283
            // if heapIndex < 0, then OOTA guarantees that we have surely
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   284
            // been removed; else we recheck under lock in remove()
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   285
            boolean cancelled = super.cancel(mayInterruptIfRunning);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   286
            if (cancelled && removeOnCancel && heapIndex >= 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   287
                remove(this);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   288
            return cancelled;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   289
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   290
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * Overrides FutureTask version so as to reset/requeue if periodic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            boolean periodic = isPeriodic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (!canRunInCurrentRunState(periodic))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            else if (!periodic)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   299
                super.run();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   300
            else if (super.runAndReset()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                setNextRunTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                reExecutePeriodic(outerTask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
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
     * Returns true if can run a task given current run state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * and run-after-shutdown parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param periodic true if this task periodic, false if delayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    boolean canRunInCurrentRunState(boolean periodic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return isRunningOrShutdown(periodic ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                   continueExistingPeriodicTasksAfterShutdown :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                   executeExistingDelayedTasksAfterShutdown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Main execution method for delayed or periodic tasks.  If pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * is shut down, rejects the task. Otherwise adds task to queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * and starts a thread, if necessary, to run it.  (We cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * prestart the thread to run the task because the task (probably)
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   324
     * shouldn't be run yet.)  If the pool is shut down while the task
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * is being added, cancel and remove it if required by state and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * run-after-shutdown parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param task the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private void delayedExecute(RunnableScheduledFuture<?> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (isShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            reject(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            super.getQueue().add(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (isShutdown() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                !canRunInCurrentRunState(task.isPeriodic()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                remove(task))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                task.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            else
10609
0caee7f44e7b 7091003: ScheduledExecutorService never executes Runnable with corePoolSize of zero
dl
parents: 9242
diff changeset
   340
                ensurePrestart();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
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
     * Requeues a periodic task unless current run state precludes it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Same idea as delayedExecute except drops task rather than rejecting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param task the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    void reExecutePeriodic(RunnableScheduledFuture<?> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (canRunInCurrentRunState(true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            super.getQueue().add(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (!canRunInCurrentRunState(true) && remove(task))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                task.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            else
10609
0caee7f44e7b 7091003: ScheduledExecutorService never executes Runnable with corePoolSize of zero
dl
parents: 9242
diff changeset
   356
                ensurePrestart();
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Cancels and clears the queue of all tasks that should not be run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * due to shutdown policy.  Invoked within super.shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    @Override void onShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        BlockingQueue<Runnable> q = super.getQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        boolean keepDelayed =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            getExecuteExistingDelayedTasksAfterShutdownPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        boolean keepPeriodic =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            getContinueExistingPeriodicTasksAfterShutdownPolicy();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   370
        if (!keepDelayed && !keepPeriodic) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   371
            for (Object e : q.toArray())
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   372
                if (e instanceof RunnableScheduledFuture<?>)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   373
                    ((RunnableScheduledFuture<?>) e).cancel(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            q.clear();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 7518
diff changeset
   375
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            // Traverse snapshot to avoid iterator exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            for (Object e : q.toArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                if (e instanceof RunnableScheduledFuture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    RunnableScheduledFuture<?> t =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        (RunnableScheduledFuture<?>)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        t.isCancelled()) { // also remove if already cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        if (q.remove(t))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            t.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        tryTerminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Modifies or replaces the task used to execute a runnable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * This method can be used to override the concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * class used for managing internal tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * The default implementation simply returns the given task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param runnable the submitted Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param task the task created to execute the runnable
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
   401
     * @param <V> the type of the task's result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return a task that can execute the runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    protected <V> RunnableScheduledFuture<V> decorateTask(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        Runnable runnable, RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Modifies or replaces the task used to execute a callable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * This method can be used to override the concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * class used for managing internal tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * The default implementation simply returns the given task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param callable the submitted Callable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param task the task created to execute the callable
19048
7d0a94c79779 8021417: Fix doclint issues in java.util.concurrent
chegar
parents: 18790
diff changeset
   418
     * @param <V> the type of the task's result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return a task that can execute the callable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    protected <V> RunnableScheduledFuture<V> decorateTask(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        Callable<V> callable, RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   428
     * The default keep-alive time for pool threads.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   429
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   430
     * Normally, this value is unused because all pool threads will be
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   431
     * core threads, but if a user creates a pool with a corePoolSize
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   432
     * of zero (against our advice), we keep a thread alive as long as
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   433
     * there are queued tasks.  If the keep alive time is zero (the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   434
     * historic value), we end up hot-spinning in getTask, wasting a
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   435
     * CPU.  But on the other hand, if we set the value too high, and
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   436
     * users create a one-shot pool which they don't cleanly shutdown,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   437
     * the pool's non-daemon threads will prevent JVM termination.  A
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   438
     * small but non-zero value (relative to a JVM's lifetime) seems
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   439
     * best.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   440
     */
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   441
    private static final long DEFAULT_KEEPALIVE_MILLIS = 10L;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   442
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   443
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * given core pool size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param corePoolSize the number of threads to keep in the pool, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public ScheduledThreadPoolExecutor(int corePoolSize) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   452
        super(corePoolSize, Integer.MAX_VALUE,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   453
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
              new DelayedWorkQueue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * given initial parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param corePoolSize the number of threads to keep in the pool, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param threadFactory the factory to use when the executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *        creates a new thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @throws NullPointerException if {@code threadFactory} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public ScheduledThreadPoolExecutor(int corePoolSize,
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   469
                                       ThreadFactory threadFactory) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   470
        super(corePoolSize, Integer.MAX_VALUE,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   471
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
              new DelayedWorkQueue(), threadFactory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   476
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   477
     * given initial parameters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param corePoolSize the number of threads to keep in the pool, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param handler the handler to use when execution is blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *        because the thread bounds and queue capacities are reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @throws NullPointerException if {@code handler} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public ScheduledThreadPoolExecutor(int corePoolSize,
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   487
                                       RejectedExecutionHandler handler) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   488
        super(corePoolSize, Integer.MAX_VALUE,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   489
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
              new DelayedWorkQueue(), handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   494
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   495
     * given initial parameters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param corePoolSize the number of threads to keep in the pool, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param threadFactory the factory to use when the executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *        creates a new thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param handler the handler to use when execution is blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *        because the thread bounds and queue capacities are reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @throws NullPointerException if {@code threadFactory} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *         {@code handler} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public ScheduledThreadPoolExecutor(int corePoolSize,
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   508
                                       ThreadFactory threadFactory,
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   509
                                       RejectedExecutionHandler handler) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   510
        super(corePoolSize, Integer.MAX_VALUE,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   511
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
              new DelayedWorkQueue(), threadFactory, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   516
     * Returns the nanoTime-based trigger time of a delayed action.
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   517
     */
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   518
    private long triggerTime(long delay, TimeUnit unit) {
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   519
        return triggerTime(unit.toNanos((delay < 0) ? 0 : delay));
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   520
    }
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   521
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   522
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   523
     * Returns the nanoTime-based trigger time of a delayed action.
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   524
     */
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   525
    long triggerTime(long delay) {
34348
ba5c2f2fc9d7 8143087: Miscellaneous changes imported from jsr166 CVS 2015-11
dl
parents: 32991
diff changeset
   526
        return System.nanoTime() +
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   527
            ((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay));
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   528
    }
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   529
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   530
    /**
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   531
     * Constrains the values of all delays in the queue to be within
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   532
     * Long.MAX_VALUE of each other, to avoid overflow in compareTo.
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   533
     * This may occur if a task is eligible to be dequeued, but has
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   534
     * not yet been, while some other task is added with a delay of
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   535
     * Long.MAX_VALUE.
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   536
     */
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   537
    private long overflowFree(long delay) {
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   538
        Delayed head = (Delayed) super.getQueue().peek();
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   539
        if (head != null) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   540
            long headDelay = head.getDelay(NANOSECONDS);
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   541
            if (headDelay < 0 && (delay - headDelay < 0))
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   542
                delay = Long.MAX_VALUE + headDelay;
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   543
        }
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   544
        return delay;
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   545
    }
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   546
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   547
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public ScheduledFuture<?> schedule(Runnable command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                       long delay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                       TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (command == null || unit == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   556
        RunnableScheduledFuture<Void> t = decorateTask(command,
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   557
            new ScheduledFutureTask<Void>(command, null,
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   558
                                          triggerTime(delay, unit),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   559
                                          sequencer.getAndIncrement()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        delayedExecute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                                           long delay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                           TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (callable == null || unit == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        RunnableScheduledFuture<V> t = decorateTask(callable,
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   574
            new ScheduledFutureTask<V>(callable,
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   575
                                       triggerTime(delay, unit),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   576
                                       sequencer.getAndIncrement()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        delayedExecute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @throws IllegalArgumentException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                                  long initialDelay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                                                  long period,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                                  TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (command == null || unit == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   592
        if (period <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        ScheduledFutureTask<Void> sft =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            new ScheduledFutureTask<Void>(command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                          null,
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   597
                                          triggerTime(initialDelay, unit),
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   598
                                          unit.toNanos(period),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   599
                                          sequencer.getAndIncrement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        RunnableScheduledFuture<Void> t = decorateTask(command, sft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        sft.outerTask = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        delayedExecute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @throws IllegalArgumentException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                                     long initialDelay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                                     long delay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                                     TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (command == null || unit == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            throw new NullPointerException();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   617
        if (delay <= 0L)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        ScheduledFutureTask<Void> sft =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            new ScheduledFutureTask<Void>(command,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                          null,
1007
72a8a27e1d69 6725789: ScheduledExecutorService does not work as expected in jdk7/6/5
dl
parents: 61
diff changeset
   622
                                          triggerTime(initialDelay, unit),
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   623
                                          -unit.toNanos(delay),
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   624
                                          sequencer.getAndIncrement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        RunnableScheduledFuture<Void> t = decorateTask(command, sft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        sft.outerTask = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        delayedExecute(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * Executes {@code command} with zero required delay.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * This has effect equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * {@link #schedule(Runnable,long,TimeUnit) schedule(command, 0, anyUnit)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Note that inspections of the queue and of the list returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * {@code shutdownNow} will access the zero-delayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * {@link ScheduledFuture}, not the {@code command} itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <p>A consequence of the use of {@code ScheduledFuture} objects is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * that {@link ThreadPoolExecutor#afterExecute afterExecute} is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * called with a null second {@code Throwable} argument, even if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * {@code command} terminated abruptly.  Instead, the {@code Throwable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * thrown by such a task can be obtained via {@link Future#get}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @throws RejectedExecutionException at discretion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *         {@code RejectedExecutionHandler}, if the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *         cannot be accepted for execution because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *         executor has been shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public void execute(Runnable command) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   652
        schedule(command, 0, NANOSECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    // Override AbstractExecutorService methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public Future<?> submit(Runnable task) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   662
        return schedule(task, 0, NANOSECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public <T> Future<T> submit(Runnable task, T result) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   670
        return schedule(Executors.callable(task, result), 0, NANOSECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @throws RejectedExecutionException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @throws NullPointerException       {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public <T> Future<T> submit(Callable<T> task) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   678
        return schedule(task, 0, NANOSECONDS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Sets the policy on whether to continue executing existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * periodic tasks even when this executor has been {@code shutdown}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * In this case, these tasks will only terminate upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * {@code shutdownNow} or after setting the policy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * {@code false} when already shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * This value is by default {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   689
     * @param value if {@code true}, continue after shutdown, else don't
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @see #getContinueExistingPeriodicTasksAfterShutdownPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        continueExistingPeriodicTasksAfterShutdown = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (!value && isShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            onShutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * Gets the policy on whether to continue executing existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * periodic tasks even when this executor has been {@code shutdown}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * In this case, these tasks will only terminate upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * {@code shutdownNow} or after setting the policy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * {@code false} when already shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * This value is by default {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return {@code true} if will continue after shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @see #setContinueExistingPeriodicTasksAfterShutdownPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return continueExistingPeriodicTasksAfterShutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Sets the policy on whether to execute existing delayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * tasks even when this executor has been {@code shutdown}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * In this case, these tasks will only terminate upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * {@code shutdownNow}, or after setting the policy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * {@code false} when already shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * This value is by default {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   721
     * @param value if {@code true}, execute after shutdown, else don't
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @see #getExecuteExistingDelayedTasksAfterShutdownPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        executeExistingDelayedTasksAfterShutdown = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (!value && isShutdown())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            onShutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Gets the policy on whether to execute existing delayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * tasks even when this executor has been {@code shutdown}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * In this case, these tasks will only terminate upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * {@code shutdownNow}, or after setting the policy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * {@code false} when already shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * This value is by default {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @return {@code true} if will execute after shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @see #setExecuteExistingDelayedTasksAfterShutdownPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return executeExistingDelayedTasksAfterShutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   746
     * Sets the policy on whether cancelled tasks should be immediately
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   747
     * removed from the work queue at time of cancellation.  This value is
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   748
     * by default {@code false}.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   749
     *
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   750
     * @param value if {@code true}, remove on cancellation, else don't
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   751
     * @see #getRemoveOnCancelPolicy
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   752
     * @since 1.7
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   753
     */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   754
    public void setRemoveOnCancelPolicy(boolean value) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   755
        removeOnCancel = value;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   756
    }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   757
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   758
    /**
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   759
     * Gets the policy on whether cancelled tasks should be immediately
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   760
     * removed from the work queue at time of cancellation.  This value is
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   761
     * by default {@code false}.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   762
     *
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   763
     * @return {@code true} if cancelled tasks are immediately removed
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   764
     *         from the queue
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   765
     * @see #setRemoveOnCancelPolicy
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   766
     * @since 1.7
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   767
     */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   768
    public boolean getRemoveOnCancelPolicy() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   769
        return removeOnCancel;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   770
    }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   771
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   772
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Initiates an orderly shutdown in which previously submitted
61
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   774
     * tasks are executed, but no new tasks will be accepted.
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   775
     * Invocation has no additional effect if already shut down.
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   776
     *
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   777
     * <p>This method does not wait for previously submitted tasks to
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   778
     * complete execution.  Use {@link #awaitTermination awaitTermination}
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   779
     * to do that.
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   780
     *
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   781
     * <p>If the {@code ExecuteExistingDelayedTasksAfterShutdownPolicy}
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   782
     * has been set {@code false}, existing delayed tasks whose delays
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   783
     * have not yet elapsed are cancelled.  And unless the {@code
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   784
     * ContinueExistingPeriodicTasksAfterShutdownPolicy} has been set
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   785
     * {@code true}, future executions of existing periodic tasks will
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   786
     * be cancelled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @throws SecurityException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        super.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Attempts to stop all actively executing tasks, halts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * processing of waiting tasks, and returns a list of the tasks
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   797
     * that were awaiting execution. These tasks are drained (removed)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   798
     * from the task queue upon return from this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
61
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   800
     * <p>This method does not wait for actively executing tasks to
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   801
     * terminate.  Use {@link #awaitTermination awaitTermination} to
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   802
     * do that.
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 59
diff changeset
   803
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <p>There are no guarantees beyond best-effort attempts to stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * processing actively executing tasks.  This implementation
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   806
     * interrupts tasks via {@link Thread#interrupt}; any task that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * fails to respond to interrupts may never terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @return list of tasks that never commenced execution.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   810
     *         Each element of this list is a {@link ScheduledFuture}.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   811
     *         For tasks submitted via one of the {@code schedule}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   812
     *         methods, the element will be identical to the returned
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   813
     *         {@code ScheduledFuture}.  For tasks submitted using
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   814
     *         {@link #execute execute}, the element will be a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *         zero-delay {@code ScheduledFuture}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @throws SecurityException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    public List<Runnable> shutdownNow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return super.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   823
     * Returns the task queue used by this executor.  Access to the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   824
     * task queue is intended primarily for debugging and monitoring.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   825
     * This queue may be in active use.  Retrieving the task queue
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   826
     * does not prevent queued tasks from executing.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   827
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   828
     * <p>Each element of this queue is a {@link ScheduledFuture}.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   829
     * For tasks submitted via one of the {@code schedule} methods, the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   830
     * element will be identical to the returned {@code ScheduledFuture}.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   831
     * For tasks submitted using {@link #execute execute}, the element
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   832
     * will be a zero-delay {@code ScheduledFuture}.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   833
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   834
     * <p>Iteration over this queue is <em>not</em> guaranteed to traverse
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   835
     * tasks in the order in which they will execute.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @return the task queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    public BlockingQueue<Runnable> getQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return super.getQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   844
     * Specialized delay queue. To mesh with TPE declarations, this
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   845
     * class must be declared as a BlockingQueue<Runnable> even though
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   846
     * it can only hold RunnableScheduledFutures.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   848
    static class DelayedWorkQueue extends AbstractQueue<Runnable>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        implements BlockingQueue<Runnable> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   851
        /*
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   852
         * A DelayedWorkQueue is based on a heap-based data structure
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   853
         * like those in DelayQueue and PriorityQueue, except that
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   854
         * every ScheduledFutureTask also records its index into the
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   855
         * heap array. This eliminates the need to find a task upon
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   856
         * cancellation, greatly speeding up removal (down from O(n)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   857
         * to O(log n)), and reducing garbage retention that would
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   858
         * otherwise occur by waiting for the element to rise to top
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   859
         * before clearing. But because the queue may also hold
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   860
         * RunnableScheduledFutures that are not ScheduledFutureTasks,
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   861
         * we are not guaranteed to have such indices available, in
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   862
         * which case we fall back to linear search. (We expect that
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   863
         * most tasks will not be decorated, and that the faster cases
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   864
         * will be much more common.)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   865
         *
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   866
         * All heap operations must record index changes -- mainly
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   867
         * within siftUp and siftDown. Upon removal, a task's
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   868
         * heapIndex is set to -1. Note that ScheduledFutureTasks can
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   869
         * appear at most once in the queue (this need not be true for
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   870
         * other kinds of tasks or work queues), so are uniquely
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   871
         * identified by heapIndex.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   872
         */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   873
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   874
        private static final int INITIAL_CAPACITY = 16;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   875
        private RunnableScheduledFuture<?>[] queue =
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   876
            new RunnableScheduledFuture<?>[INITIAL_CAPACITY];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   877
        private final ReentrantLock lock = new ReentrantLock();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   878
        private int size;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   879
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   880
        /**
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   881
         * Thread designated to wait for the task at the head of the
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   882
         * queue.  This variant of the Leader-Follower pattern
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   883
         * (http://www.cs.wustl.edu/~schmidt/POSA/POSA2/) serves to
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   884
         * minimize unnecessary timed waiting.  When a thread becomes
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   885
         * the leader, it waits only for the next delay to elapse, but
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   886
         * other threads await indefinitely.  The leader thread must
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   887
         * signal some other thread before returning from take() or
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   888
         * poll(...), unless some other thread becomes leader in the
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   889
         * interim.  Whenever the head of the queue is replaced with a
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   890
         * task with an earlier expiration time, the leader field is
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   891
         * invalidated by being reset to null, and some waiting
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   892
         * thread, but not necessarily the current leader, is
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   893
         * signalled.  So waiting threads must be prepared to acquire
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   894
         * and lose leadership while waiting.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   895
         */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
   896
        private Thread leader;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   897
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   898
        /**
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   899
         * Condition signalled when a newer task becomes available at the
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   900
         * head of the queue or a new thread may need to become leader.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   901
         */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   902
        private final Condition available = lock.newCondition();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   903
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   904
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   905
         * Sets f's heapIndex if it is a ScheduledFutureTask.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   906
         */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   907
        private void setIndex(RunnableScheduledFuture<?> f, int idx) {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   908
            if (f instanceof ScheduledFutureTask)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   909
                ((ScheduledFutureTask)f).heapIndex = idx;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   910
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   911
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   912
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   913
         * Sifts element added at bottom up to its heap-ordered spot.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   914
         * Call only when holding lock.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   915
         */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   916
        private void siftUp(int k, RunnableScheduledFuture<?> key) {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   917
            while (k > 0) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   918
                int parent = (k - 1) >>> 1;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   919
                RunnableScheduledFuture<?> e = queue[parent];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   920
                if (key.compareTo(e) >= 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   921
                    break;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   922
                queue[k] = e;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   923
                setIndex(e, k);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   924
                k = parent;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   925
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   926
            queue[k] = key;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   927
            setIndex(key, k);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   928
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   929
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   930
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   931
         * Sifts element added at top down to its heap-ordered spot.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   932
         * Call only when holding lock.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   933
         */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   934
        private void siftDown(int k, RunnableScheduledFuture<?> key) {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   935
            int half = size >>> 1;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   936
            while (k < half) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   937
                int child = (k << 1) + 1;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
   938
                RunnableScheduledFuture<?> c = queue[child];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   939
                int right = child + 1;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   940
                if (right < size && c.compareTo(queue[right]) > 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   941
                    c = queue[child = right];
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   942
                if (key.compareTo(c) <= 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   943
                    break;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   944
                queue[k] = c;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   945
                setIndex(c, k);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   946
                k = child;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   947
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   948
            queue[k] = key;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   949
            setIndex(key, k);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   950
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   951
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   952
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   953
         * Resizes the heap array.  Call only when holding lock.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   954
         */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   955
        private void grow() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   956
            int oldCapacity = queue.length;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   957
            int newCapacity = oldCapacity + (oldCapacity >> 1); // grow 50%
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   958
            if (newCapacity < 0) // overflow
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   959
                newCapacity = Integer.MAX_VALUE;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   960
            queue = Arrays.copyOf(queue, newCapacity);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   961
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   962
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   963
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
   964
         * Finds index of given object, or -1 if absent.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   965
         */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   966
        private int indexOf(Object x) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   967
            if (x != null) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   968
                if (x instanceof ScheduledFutureTask) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   969
                    int i = ((ScheduledFutureTask) x).heapIndex;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   970
                    // Sanity check; x could conceivably be a
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   971
                    // ScheduledFutureTask from some other pool.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   972
                    if (i >= 0 && i < size && queue[i] == x)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   973
                        return i;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   974
                } else {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   975
                    for (int i = 0; i < size; i++)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   976
                        if (x.equals(queue[i]))
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   977
                            return i;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   978
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   979
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   980
            return -1;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   981
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   982
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   983
        public boolean contains(Object x) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   984
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   985
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   986
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   987
                return indexOf(x) != -1;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   988
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   989
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   990
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   991
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   992
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   993
        public boolean remove(Object x) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   994
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   995
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   996
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   997
                int i = indexOf(x);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   998
                if (i < 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
   999
                    return false;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1000
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1001
                setIndex(queue[i], -1);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1002
                int s = --size;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1003
                RunnableScheduledFuture<?> replacement = queue[s];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1004
                queue[s] = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1005
                if (s != i) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1006
                    siftDown(i, replacement);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1007
                    if (queue[i] == replacement)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1008
                        siftUp(i, replacement);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1009
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1010
                return true;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1011
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1012
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1013
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1014
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1015
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1016
        public int size() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1017
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1018
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1019
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1020
                return size;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1021
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1022
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1023
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1024
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1025
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1026
        public boolean isEmpty() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1027
            return size() == 0;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1028
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1029
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1030
        public int remainingCapacity() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1031
            return Integer.MAX_VALUE;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1032
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1033
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1034
        public RunnableScheduledFuture<?> peek() {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1035
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1036
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1037
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1038
                return queue[0];
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1039
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1040
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1041
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1042
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1043
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1044
        public boolean offer(Runnable x) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1045
            if (x == null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1046
                throw new NullPointerException();
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1047
            RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>)x;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1048
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1049
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1050
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1051
                int i = size;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1052
                if (i >= queue.length)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1053
                    grow();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1054
                size = i + 1;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1055
                if (i == 0) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1056
                    queue[0] = e;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1057
                    setIndex(e, 0);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1058
                } else {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1059
                    siftUp(i, e);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1060
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1061
                if (queue[0] == e) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1062
                    leader = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1063
                    available.signal();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1064
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1065
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1066
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1067
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1068
            return true;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1069
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1070
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1071
        public void put(Runnable e) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1072
            offer(e);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1073
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1074
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1075
        public boolean add(Runnable e) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1076
            return offer(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1079
        public boolean offer(Runnable e, long timeout, TimeUnit unit) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1080
            return offer(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1082
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1083
        /**
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1084
         * Performs common bookkeeping for poll and take: Replaces
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1085
         * first element with last and sifts it down.  Call only when
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1086
         * holding lock.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1087
         * @param f the task to remove and return
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1088
         */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1089
        private RunnableScheduledFuture<?> finishPoll(RunnableScheduledFuture<?> f) {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1090
            int s = --size;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1091
            RunnableScheduledFuture<?> x = queue[s];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1092
            queue[s] = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1093
            if (s != 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1094
                siftDown(0, x);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1095
            setIndex(f, -1);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1096
            return f;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1097
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1098
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1099
        public RunnableScheduledFuture<?> poll() {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1100
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1101
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1102
            try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1103
                RunnableScheduledFuture<?> first = queue[0];
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1104
                return (first == null || first.getDelay(NANOSECONDS) > 0)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1105
                    ? null
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1106
                    : finishPoll(first);
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1107
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1108
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1109
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1111
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1112
        public RunnableScheduledFuture<?> take() throws InterruptedException {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1113
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1114
            lock.lockInterruptibly();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1115
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1116
                for (;;) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1117
                    RunnableScheduledFuture<?> first = queue[0];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1118
                    if (first == null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1119
                        available.await();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1120
                    else {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1121
                        long delay = first.getDelay(NANOSECONDS);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1122
                        if (delay <= 0L)
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1123
                            return finishPoll(first);
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
  1124
                        first = null; // don't retain ref while waiting
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
  1125
                        if (leader != null)
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1126
                            available.await();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1127
                        else {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1128
                            Thread thisThread = Thread.currentThread();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1129
                            leader = thisThread;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1130
                            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1131
                                available.awaitNanos(delay);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1132
                            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1133
                                if (leader == thisThread)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1134
                                    leader = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1135
                            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1136
                        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1137
                    }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1138
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1139
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1140
                if (leader == null && queue[0] != null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1141
                    available.signal();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1142
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1143
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1145
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1146
        public RunnableScheduledFuture<?> poll(long timeout, TimeUnit unit)
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1147
            throws InterruptedException {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1148
            long nanos = unit.toNanos(timeout);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1149
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1150
            lock.lockInterruptibly();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1151
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1152
                for (;;) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1153
                    RunnableScheduledFuture<?> first = queue[0];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1154
                    if (first == null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1155
                        if (nanos <= 0L)
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1156
                            return null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1157
                        else
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1158
                            nanos = available.awaitNanos(nanos);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1159
                    } else {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1160
                        long delay = first.getDelay(NANOSECONDS);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1161
                        if (delay <= 0L)
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1162
                            return finishPoll(first);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1163
                        if (nanos <= 0L)
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1164
                            return null;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
  1165
                        first = null; // don't retain ref while waiting
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1166
                        if (nanos < delay || leader != null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1167
                            nanos = available.awaitNanos(nanos);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1168
                        else {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1169
                            Thread thisThread = Thread.currentThread();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1170
                            leader = thisThread;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1171
                            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1172
                                long timeLeft = available.awaitNanos(delay);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1173
                                nanos -= delay - timeLeft;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1174
                            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1175
                                if (leader == thisThread)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1176
                                    leader = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1177
                            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1178
                        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1179
                    }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1180
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1181
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1182
                if (leader == null && queue[0] != null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1183
                    available.signal();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1184
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1185
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1188
        public void clear() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1189
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1190
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1191
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1192
                for (int i = 0; i < size; i++) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1193
                    RunnableScheduledFuture<?> t = queue[i];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1194
                    if (t != null) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1195
                        queue[i] = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1196
                        setIndex(t, -1);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1197
                    }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1198
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1199
                size = 0;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1200
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1201
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1202
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1203
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1204
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1205
        /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1206
         * Returns first element only if it is expired.
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1207
         * Used only by drainTo.  Call only when holding lock.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1208
         */
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1209
        private RunnableScheduledFuture<?> peekExpired() {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1210
            // assert lock.isHeldByCurrentThread();
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1211
            RunnableScheduledFuture<?> first = queue[0];
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1212
            return (first == null || first.getDelay(NANOSECONDS) > 0) ?
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1213
                null : first;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1214
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1215
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1216
        public int drainTo(Collection<? super Runnable> c) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1217
            if (c == null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1218
                throw new NullPointerException();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1219
            if (c == this)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1220
                throw new IllegalArgumentException();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1221
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1222
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1223
            try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1224
                RunnableScheduledFuture<?> first;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1225
                int n = 0;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1226
                while ((first = peekExpired()) != null) {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1227
                    c.add(first);   // In this order, in case add() throws.
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1228
                    finishPoll(first);
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1229
                    ++n;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1230
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1231
                return n;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1232
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1233
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1234
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1237
        public int drainTo(Collection<? super Runnable> c, int maxElements) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1238
            if (c == null)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1239
                throw new NullPointerException();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1240
            if (c == this)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1241
                throw new IllegalArgumentException();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1242
            if (maxElements <= 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1243
                return 0;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1244
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1245
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1246
            try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 10609
diff changeset
  1247
                RunnableScheduledFuture<?> first;
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1248
                int n = 0;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1249
                while (n < maxElements && (first = peekExpired()) != null) {
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1250
                    c.add(first);   // In this order, in case add() throws.
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1251
                    finishPoll(first);
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1252
                    ++n;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1253
                }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1254
                return n;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1255
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1256
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1257
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1258
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1259
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1260
        public Object[] toArray() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1261
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1262
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1263
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1264
                return Arrays.copyOf(queue, size, Object[].class);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1265
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1266
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1267
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1268
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1269
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1270
        @SuppressWarnings("unchecked")
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1271
        public <T> T[] toArray(T[] a) {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1272
            final ReentrantLock lock = this.lock;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1273
            lock.lock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1274
            try {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1275
                if (a.length < size)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1276
                    return (T[]) Arrays.copyOf(queue, size, a.getClass());
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1277
                System.arraycopy(queue, 0, a, 0, size);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1278
                if (a.length > size)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1279
                    a[size] = null;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1280
                return a;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1281
            } finally {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1282
                lock.unlock();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1283
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1284
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1285
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        public Iterator<Runnable> iterator() {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1287
            return new Itr(Arrays.copyOf(queue, size));
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1288
        }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1289
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1290
        /**
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1291
         * Snapshot iterator that works off copy of underlying q array.
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1292
         */
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1293
        private class Itr implements Iterator<Runnable> {
21667
e4c61eb98292 8028300: Fix raw type lint warnings in java.util.concurrent
darcy
parents: 19048
diff changeset
  1294
            final RunnableScheduledFuture<?>[] array;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1295
            int cursor;        // index of next element to return; initially 0
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
  1296
            int lastRet = -1;  // index of last element returned; -1 if no such
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1297
21667
e4c61eb98292 8028300: Fix raw type lint warnings in java.util.concurrent
darcy
parents: 19048
diff changeset
  1298
            Itr(RunnableScheduledFuture<?>[] array) {
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1299
                this.array = array;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1300
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1301
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1302
            public boolean hasNext() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1303
                return cursor < array.length;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1304
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1305
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1306
            public Runnable next() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1307
                if (cursor >= array.length)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1308
                    throw new NoSuchElementException();
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 34348
diff changeset
  1309
                return array[lastRet = cursor++];
59
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1310
            }
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1311
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1312
            public void remove() {
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1313
                if (lastRet < 0)
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1314
                    throw new IllegalStateException();
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1315
                DelayedWorkQueue.this.remove(array[lastRet]);
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1316
                lastRet = -1;
f35c86a7e82d 6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents: 2
diff changeset
  1317
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
}