author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 34348 | ba5c2f2fc9d7 |
child 45936 | d564ef4bed8f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
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 | 34 |
*/ |
35 |
||
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 | 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 | 47 |
import java.util.concurrent.atomic.AtomicLong; |
48 |
import java.util.concurrent.locks.Condition; |
|
49 |
import java.util.concurrent.locks.ReentrantLock; |
|
2 | 50 |
|
51 |
/** |
|
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 | 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 | 60 |
* without any real-time guarantees about when, after they are |
61 |
* enabled, they will commence. Tasks scheduled for exactly the same |
|
62 |
* execution time are enabled in first-in-first-out (FIFO) order of |
|
63 |
* submission. |
|
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 | 79 |
* those of subsequent ones. |
80 |
* |
|
2 | 81 |
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
82 |
* of the inherited tuning methods are not useful for it. In |
|
83 |
* particular, because it acts as a fixed-sized pool using |
|
84 |
* {@code corePoolSize} threads and an unbounded queue, adjustments |
|
85 |
* to {@code maximumPoolSize} have no useful effect. Additionally, it |
|
86 |
* is almost never a good idea to set {@code corePoolSize} to zero or |
|
87 |
* use {@code allowCoreThreadTimeOut} because this may leave the pool |
|
88 |
* without threads to handle tasks once they become eligible to run. |
|
89 |
* |
|
90 |
* <p><b>Extension notes:</b> This class overrides the |
|
18790 | 91 |
* {@link ThreadPoolExecutor#execute(Runnable) execute} and |
2 | 92 |
* {@link AbstractExecutorService#submit(Runnable) submit} |
93 |
* methods to generate internal {@link ScheduledFuture} objects to |
|
94 |
* control per-task delays and scheduling. To preserve |
|
95 |
* functionality, any further overrides of these methods in |
|
96 |
* subclasses must invoke superclass versions, which effectively |
|
97 |
* disables additional task customization. However, this class |
|
98 |
* provides alternative protected extension method |
|
99 |
* {@code decorateTask} (one version each for {@code Runnable} and |
|
100 |
* {@code Callable}) that can be used to customize the concrete task |
|
101 |
* types used to execute commands entered via {@code execute}, |
|
102 |
* {@code submit}, {@code schedule}, {@code scheduleAtFixedRate}, |
|
103 |
* and {@code scheduleWithFixedDelay}. By default, a |
|
104 |
* {@code ScheduledThreadPoolExecutor} uses a task type extending |
|
105 |
* {@link FutureTask}. However, this may be modified or replaced using |
|
106 |
* subclasses of the form: |
|
107 |
* |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
108 |
* <pre> {@code |
2 | 109 |
* public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor { |
110 |
* |
|
111 |
* static class CustomTask<V> implements RunnableScheduledFuture<V> { ... } |
|
112 |
* |
|
113 |
* protected <V> RunnableScheduledFuture<V> decorateTask( |
|
114 |
* Runnable r, RunnableScheduledFuture<V> task) { |
|
115 |
* return new CustomTask<V>(r, task); |
|
116 |
* } |
|
117 |
* |
|
118 |
* protected <V> RunnableScheduledFuture<V> decorateTask( |
|
119 |
* Callable<V> c, RunnableScheduledFuture<V> task) { |
|
120 |
* return new CustomTask<V>(c, task); |
|
121 |
* } |
|
122 |
* // ... add constructors, etc. |
|
123 |
* }}</pre> |
|
124 |
* |
|
125 |
* @since 1.5 |
|
126 |
* @author Doug Lea |
|
127 |
*/ |
|
128 |
public class ScheduledThreadPoolExecutor |
|
129 |
extends ThreadPoolExecutor |
|
130 |
implements ScheduledExecutorService { |
|
131 |
||
132 |
/* |
|
133 |
* This class specializes ThreadPoolExecutor implementation by |
|
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 | 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 | 141 |
* unbounded DelayQueue. The lack of capacity constraint and |
142 |
* the fact that corePoolSize and maximumPoolSize are |
|
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 | 145 |
* |
146 |
* 3. Supporting optional run-after-shutdown parameters, which |
|
147 |
* leads to overrides of shutdown methods to remove and cancel |
|
148 |
* tasks that should NOT be run after shutdown, as well as |
|
149 |
* different recheck logic when task (re)submission overlaps |
|
150 |
* with a shutdown. |
|
151 |
* |
|
152 |
* 4. Task decoration methods to allow interception and |
|
153 |
* instrumentation, which are needed because subclasses cannot |
|
154 |
* otherwise override submit methods to get this effect. These |
|
155 |
* don't have any impact on pool control logic though. |
|
156 |
*/ |
|
157 |
||
158 |
/** |
|
159 |
* False if should cancel/suppress periodic tasks on shutdown. |
|
160 |
*/ |
|
161 |
private volatile boolean continueExistingPeriodicTasksAfterShutdown; |
|
162 |
||
163 |
/** |
|
164 |
* False if should cancel non-periodic tasks on shutdown. |
|
165 |
*/ |
|
166 |
private volatile boolean executeExistingDelayedTasksAfterShutdown = true; |
|
167 |
||
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 | 174 |
* Sequence number to break scheduling ties, and in turn to |
175 |
* guarantee FIFO order among tied entries. |
|
176 |
*/ |
|
11279 | 177 |
private static final AtomicLong sequencer = new AtomicLong(); |
2 | 178 |
|
179 |
private class ScheduledFutureTask<V> |
|
180 |
extends FutureTask<V> implements RunnableScheduledFuture<V> { |
|
181 |
||
182 |
/** Sequence number to break ties FIFO */ |
|
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 | 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 | 193 |
*/ |
194 |
private final long period; |
|
195 |
||
196 |
/** The actual task to be re-enqueued by reExecutePeriodic */ |
|
197 |
RunnableScheduledFuture<V> outerTask = this; |
|
198 |
||
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 | 205 |
* Creates a one-shot action with given nanoTime-based trigger time. |
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 | 209 |
super(r, result); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
210 |
this.time = triggerTime; |
2 | 211 |
this.period = 0; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
212 |
this.sequenceNumber = sequenceNumber; |
2 | 213 |
} |
214 |
||
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 | 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 | 221 |
super(r, result); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
222 |
this.time = triggerTime; |
2 | 223 |
this.period = period; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
224 |
this.sequenceNumber = sequenceNumber; |
2 | 225 |
} |
226 |
||
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 | 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 | 232 |
super(callable); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
233 |
this.time = triggerTime; |
2 | 234 |
this.period = 0; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
235 |
this.sequenceNumber = sequenceNumber; |
2 | 236 |
} |
237 |
||
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 | 240 |
} |
241 |
||
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 | 244 |
return 0; |
245 |
if (other instanceof ScheduledFutureTask) { |
|
246 |
ScheduledFutureTask<?> x = (ScheduledFutureTask<?>)other; |
|
247 |
long diff = time - x.time; |
|
248 |
if (diff < 0) |
|
249 |
return -1; |
|
250 |
else if (diff > 0) |
|
251 |
return 1; |
|
252 |
else if (sequenceNumber < x.sequenceNumber) |
|
253 |
return -1; |
|
254 |
else |
|
255 |
return 1; |
|
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 | 259 |
} |
260 |
||
261 |
/** |
|
18790 | 262 |
* Returns {@code true} if this is a periodic (not a one-shot) action. |
2 | 263 |
* |
18790 | 264 |
* @return {@code true} if periodic |
2 | 265 |
*/ |
266 |
public boolean isPeriodic() { |
|
267 |
return period != 0; |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Sets the next time to run for a periodic task. |
|
272 |
*/ |
|
273 |
private void setNextRunTime() { |
|
274 |
long p = period; |
|
275 |
if (p > 0) |
|
276 |
time += p; |
|
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 | 279 |
} |
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 | 291 |
/** |
292 |
* Overrides FutureTask version so as to reset/requeue if periodic. |
|
293 |
*/ |
|
294 |
public void run() { |
|
295 |
boolean periodic = isPeriodic(); |
|
296 |
if (!canRunInCurrentRunState(periodic)) |
|
297 |
cancel(false); |
|
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 | 301 |
setNextRunTime(); |
302 |
reExecutePeriodic(outerTask); |
|
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Returns true if can run a task given current run state |
|
309 |
* and run-after-shutdown parameters. |
|
310 |
* |
|
311 |
* @param periodic true if this task periodic, false if delayed |
|
312 |
*/ |
|
313 |
boolean canRunInCurrentRunState(boolean periodic) { |
|
314 |
return isRunningOrShutdown(periodic ? |
|
315 |
continueExistingPeriodicTasksAfterShutdown : |
|
316 |
executeExistingDelayedTasksAfterShutdown); |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* Main execution method for delayed or periodic tasks. If pool |
|
321 |
* is shut down, rejects the task. Otherwise adds task to queue |
|
322 |
* and starts a thread, if necessary, to run it. (We cannot |
|
323 |
* prestart the thread to run the task because the task (probably) |
|
18790 | 324 |
* shouldn't be run yet.) If the pool is shut down while the task |
2 | 325 |
* is being added, cancel and remove it if required by state and |
326 |
* run-after-shutdown parameters. |
|
327 |
* |
|
328 |
* @param task the task |
|
329 |
*/ |
|
330 |
private void delayedExecute(RunnableScheduledFuture<?> task) { |
|
331 |
if (isShutdown()) |
|
332 |
reject(task); |
|
333 |
else { |
|
334 |
super.getQueue().add(task); |
|
335 |
if (isShutdown() && |
|
336 |
!canRunInCurrentRunState(task.isPeriodic()) && |
|
337 |
remove(task)) |
|
338 |
task.cancel(false); |
|
339 |
else |
|
10609
0caee7f44e7b
7091003: ScheduledExecutorService never executes Runnable with corePoolSize of zero
dl
parents:
9242
diff
changeset
|
340 |
ensurePrestart(); |
2 | 341 |
} |
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Requeues a periodic task unless current run state precludes it. |
|
346 |
* Same idea as delayedExecute except drops task rather than rejecting. |
|
347 |
* |
|
348 |
* @param task the task |
|
349 |
*/ |
|
350 |
void reExecutePeriodic(RunnableScheduledFuture<?> task) { |
|
351 |
if (canRunInCurrentRunState(true)) { |
|
352 |
super.getQueue().add(task); |
|
353 |
if (!canRunInCurrentRunState(true) && remove(task)) |
|
354 |
task.cancel(false); |
|
355 |
else |
|
10609
0caee7f44e7b
7091003: ScheduledExecutorService never executes Runnable with corePoolSize of zero
dl
parents:
9242
diff
changeset
|
356 |
ensurePrestart(); |
2 | 357 |
} |
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Cancels and clears the queue of all tasks that should not be run |
|
362 |
* due to shutdown policy. Invoked within super.shutdown. |
|
363 |
*/ |
|
364 |
@Override void onShutdown() { |
|
365 |
BlockingQueue<Runnable> q = super.getQueue(); |
|
366 |
boolean keepDelayed = |
|
367 |
getExecuteExistingDelayedTasksAfterShutdownPolicy(); |
|
368 |
boolean keepPeriodic = |
|
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 | 374 |
q.clear(); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7518
diff
changeset
|
375 |
} |
2 | 376 |
else { |
377 |
// Traverse snapshot to avoid iterator exceptions |
|
378 |
for (Object e : q.toArray()) { |
|
379 |
if (e instanceof RunnableScheduledFuture) { |
|
380 |
RunnableScheduledFuture<?> t = |
|
381 |
(RunnableScheduledFuture<?>)e; |
|
382 |
if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed) || |
|
383 |
t.isCancelled()) { // also remove if already cancelled |
|
384 |
if (q.remove(t)) |
|
385 |
t.cancel(false); |
|
386 |
} |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
tryTerminate(); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Modifies or replaces the task used to execute a runnable. |
|
395 |
* This method can be used to override the concrete |
|
396 |
* class used for managing internal tasks. |
|
397 |
* The default implementation simply returns the given task. |
|
398 |
* |
|
399 |
* @param runnable the submitted Runnable |
|
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 | 402 |
* @return a task that can execute the runnable |
403 |
* @since 1.6 |
|
404 |
*/ |
|
405 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
|
406 |
Runnable runnable, RunnableScheduledFuture<V> task) { |
|
407 |
return task; |
|
408 |
} |
|
409 |
||
410 |
/** |
|
411 |
* Modifies or replaces the task used to execute a callable. |
|
412 |
* This method can be used to override the concrete |
|
413 |
* class used for managing internal tasks. |
|
414 |
* The default implementation simply returns the given task. |
|
415 |
* |
|
416 |
* @param callable the submitted Callable |
|
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 | 419 |
* @return a task that can execute the callable |
420 |
* @since 1.6 |
|
421 |
*/ |
|
422 |
protected <V> RunnableScheduledFuture<V> decorateTask( |
|
423 |
Callable<V> callable, RunnableScheduledFuture<V> task) { |
|
424 |
return task; |
|
425 |
} |
|
426 |
||
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 | 444 |
* Creates a new {@code ScheduledThreadPoolExecutor} with the |
445 |
* given core pool size. |
|
446 |
* |
|
447 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
448 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
449 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
450 |
*/ |
|
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 | 454 |
new DelayedWorkQueue()); |
455 |
} |
|
456 |
||
457 |
/** |
|
458 |
* Creates a new {@code ScheduledThreadPoolExecutor} with the |
|
459 |
* given initial parameters. |
|
460 |
* |
|
461 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
462 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
463 |
* @param threadFactory the factory to use when the executor |
|
464 |
* creates a new thread |
|
465 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
466 |
* @throws NullPointerException if {@code threadFactory} is null |
|
467 |
*/ |
|
468 |
public ScheduledThreadPoolExecutor(int corePoolSize, |
|
7518 | 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 | 472 |
new DelayedWorkQueue(), threadFactory); |
473 |
} |
|
474 |
||
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 | 478 |
* |
479 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
480 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
481 |
* @param handler the handler to use when execution is blocked |
|
482 |
* because the thread bounds and queue capacities are reached |
|
483 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
484 |
* @throws NullPointerException if {@code handler} is null |
|
485 |
*/ |
|
486 |
public ScheduledThreadPoolExecutor(int corePoolSize, |
|
7518 | 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 | 490 |
new DelayedWorkQueue(), handler); |
491 |
} |
|
492 |
||
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 | 496 |
* |
497 |
* @param corePoolSize the number of threads to keep in the pool, even |
|
498 |
* if they are idle, unless {@code allowCoreThreadTimeOut} is set |
|
499 |
* @param threadFactory the factory to use when the executor |
|
500 |
* creates a new thread |
|
501 |
* @param handler the handler to use when execution is blocked |
|
502 |
* because the thread bounds and queue capacities are reached |
|
503 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
|
504 |
* @throws NullPointerException if {@code threadFactory} or |
|
505 |
* {@code handler} is null |
|
506 |
*/ |
|
507 |
public ScheduledThreadPoolExecutor(int corePoolSize, |
|
7518 | 508 |
ThreadFactory threadFactory, |
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 | 512 |
new DelayedWorkQueue(), threadFactory, handler); |
513 |
} |
|
514 |
||
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 | 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 | 548 |
* @throws RejectedExecutionException {@inheritDoc} |
549 |
* @throws NullPointerException {@inheritDoc} |
|
550 |
*/ |
|
551 |
public ScheduledFuture<?> schedule(Runnable command, |
|
552 |
long delay, |
|
553 |
TimeUnit unit) { |
|
554 |
if (command == null || unit == null) |
|
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 | 560 |
delayedExecute(t); |
561 |
return t; |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* @throws RejectedExecutionException {@inheritDoc} |
|
566 |
* @throws NullPointerException {@inheritDoc} |
|
567 |
*/ |
|
568 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, |
|
569 |
long delay, |
|
570 |
TimeUnit unit) { |
|
571 |
if (callable == null || unit == null) |
|
572 |
throw new NullPointerException(); |
|
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 | 577 |
delayedExecute(t); |
578 |
return t; |
|
579 |
} |
|
580 |
||
581 |
/** |
|
582 |
* @throws RejectedExecutionException {@inheritDoc} |
|
583 |
* @throws NullPointerException {@inheritDoc} |
|
584 |
* @throws IllegalArgumentException {@inheritDoc} |
|
585 |
*/ |
|
586 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, |
|
587 |
long initialDelay, |
|
588 |
long period, |
|
589 |
TimeUnit unit) { |
|
590 |
if (command == null || unit == null) |
|
591 |
throw new NullPointerException(); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
592 |
if (period <= 0L) |
2 | 593 |
throw new IllegalArgumentException(); |
594 |
ScheduledFutureTask<Void> sft = |
|
595 |
new ScheduledFutureTask<Void>(command, |
|
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 | 600 |
RunnableScheduledFuture<Void> t = decorateTask(command, sft); |
601 |
sft.outerTask = t; |
|
602 |
delayedExecute(t); |
|
603 |
return t; |
|
604 |
} |
|
605 |
||
606 |
/** |
|
607 |
* @throws RejectedExecutionException {@inheritDoc} |
|
608 |
* @throws NullPointerException {@inheritDoc} |
|
609 |
* @throws IllegalArgumentException {@inheritDoc} |
|
610 |
*/ |
|
611 |
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, |
|
612 |
long initialDelay, |
|
613 |
long delay, |
|
614 |
TimeUnit unit) { |
|
615 |
if (command == null || unit == null) |
|
616 |
throw new NullPointerException(); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
617 |
if (delay <= 0L) |
2 | 618 |
throw new IllegalArgumentException(); |
619 |
ScheduledFutureTask<Void> sft = |
|
620 |
new ScheduledFutureTask<Void>(command, |
|
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 | 625 |
RunnableScheduledFuture<Void> t = decorateTask(command, sft); |
626 |
sft.outerTask = t; |
|
627 |
delayedExecute(t); |
|
628 |
return t; |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* Executes {@code command} with zero required delay. |
|
633 |
* This has effect equivalent to |
|
634 |
* {@link #schedule(Runnable,long,TimeUnit) schedule(command, 0, anyUnit)}. |
|
635 |
* Note that inspections of the queue and of the list returned by |
|
636 |
* {@code shutdownNow} will access the zero-delayed |
|
637 |
* {@link ScheduledFuture}, not the {@code command} itself. |
|
638 |
* |
|
639 |
* <p>A consequence of the use of {@code ScheduledFuture} objects is |
|
640 |
* that {@link ThreadPoolExecutor#afterExecute afterExecute} is always |
|
641 |
* called with a null second {@code Throwable} argument, even if the |
|
642 |
* {@code command} terminated abruptly. Instead, the {@code Throwable} |
|
643 |
* thrown by such a task can be obtained via {@link Future#get}. |
|
644 |
* |
|
645 |
* @throws RejectedExecutionException at discretion of |
|
646 |
* {@code RejectedExecutionHandler}, if the task |
|
647 |
* cannot be accepted for execution because the |
|
648 |
* executor has been shut down |
|
649 |
* @throws NullPointerException {@inheritDoc} |
|
650 |
*/ |
|
651 |
public void execute(Runnable command) { |
|
11279 | 652 |
schedule(command, 0, NANOSECONDS); |
2 | 653 |
} |
654 |
||
655 |
// Override AbstractExecutorService methods |
|
656 |
||
657 |
/** |
|
658 |
* @throws RejectedExecutionException {@inheritDoc} |
|
659 |
* @throws NullPointerException {@inheritDoc} |
|
660 |
*/ |
|
661 |
public Future<?> submit(Runnable task) { |
|
11279 | 662 |
return schedule(task, 0, NANOSECONDS); |
2 | 663 |
} |
664 |
||
665 |
/** |
|
666 |
* @throws RejectedExecutionException {@inheritDoc} |
|
667 |
* @throws NullPointerException {@inheritDoc} |
|
668 |
*/ |
|
669 |
public <T> Future<T> submit(Runnable task, T result) { |
|
11279 | 670 |
return schedule(Executors.callable(task, result), 0, NANOSECONDS); |
2 | 671 |
} |
672 |
||
673 |
/** |
|
674 |
* @throws RejectedExecutionException {@inheritDoc} |
|
675 |
* @throws NullPointerException {@inheritDoc} |
|
676 |
*/ |
|
677 |
public <T> Future<T> submit(Callable<T> task) { |
|
11279 | 678 |
return schedule(task, 0, NANOSECONDS); |
2 | 679 |
} |
680 |
||
681 |
/** |
|
682 |
* Sets the policy on whether to continue executing existing |
|
683 |
* periodic tasks even when this executor has been {@code shutdown}. |
|
684 |
* In this case, these tasks will only terminate upon |
|
685 |
* {@code shutdownNow} or after setting the policy to |
|
686 |
* {@code false} when already shutdown. |
|
687 |
* This value is by default {@code false}. |
|
688 |
* |
|
18790 | 689 |
* @param value if {@code true}, continue after shutdown, else don't |
2 | 690 |
* @see #getContinueExistingPeriodicTasksAfterShutdownPolicy |
691 |
*/ |
|
692 |
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) { |
|
693 |
continueExistingPeriodicTasksAfterShutdown = value; |
|
694 |
if (!value && isShutdown()) |
|
695 |
onShutdown(); |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* Gets the policy on whether to continue executing existing |
|
700 |
* periodic tasks even when this executor has been {@code shutdown}. |
|
701 |
* In this case, these tasks will only terminate upon |
|
702 |
* {@code shutdownNow} or after setting the policy to |
|
703 |
* {@code false} when already shutdown. |
|
704 |
* This value is by default {@code false}. |
|
705 |
* |
|
706 |
* @return {@code true} if will continue after shutdown |
|
707 |
* @see #setContinueExistingPeriodicTasksAfterShutdownPolicy |
|
708 |
*/ |
|
709 |
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() { |
|
710 |
return continueExistingPeriodicTasksAfterShutdown; |
|
711 |
} |
|
712 |
||
713 |
/** |
|
714 |
* Sets the policy on whether to execute existing delayed |
|
715 |
* tasks even when this executor has been {@code shutdown}. |
|
716 |
* In this case, these tasks will only terminate upon |
|
717 |
* {@code shutdownNow}, or after setting the policy to |
|
718 |
* {@code false} when already shutdown. |
|
719 |
* This value is by default {@code true}. |
|
720 |
* |
|
18790 | 721 |
* @param value if {@code true}, execute after shutdown, else don't |
2 | 722 |
* @see #getExecuteExistingDelayedTasksAfterShutdownPolicy |
723 |
*/ |
|
724 |
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) { |
|
725 |
executeExistingDelayedTasksAfterShutdown = value; |
|
726 |
if (!value && isShutdown()) |
|
727 |
onShutdown(); |
|
728 |
} |
|
729 |
||
730 |
/** |
|
731 |
* Gets the policy on whether to execute existing delayed |
|
732 |
* tasks even when this executor has been {@code shutdown}. |
|
733 |
* In this case, these tasks will only terminate upon |
|
734 |
* {@code shutdownNow}, or after setting the policy to |
|
735 |
* {@code false} when already shutdown. |
|
736 |
* This value is by default {@code true}. |
|
737 |
* |
|
738 |
* @return {@code true} if will execute after shutdown |
|
739 |
* @see #setExecuteExistingDelayedTasksAfterShutdownPolicy |
|
740 |
*/ |
|
741 |
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() { |
|
742 |
return executeExistingDelayedTasksAfterShutdown; |
|
743 |
} |
|
744 |
||
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 | 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 | 787 |
* |
788 |
* @throws SecurityException {@inheritDoc} |
|
789 |
*/ |
|
790 |
public void shutdown() { |
|
791 |
super.shutdown(); |
|
792 |
} |
|
793 |
||
794 |
/** |
|
795 |
* Attempts to stop all actively executing tasks, halts the |
|
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 | 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 | 804 |
* <p>There are no guarantees beyond best-effort attempts to stop |
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 | 807 |
* fails to respond to interrupts may never terminate. |
808 |
* |
|
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 | 815 |
* zero-delay {@code ScheduledFuture}. |
816 |
* @throws SecurityException {@inheritDoc} |
|
817 |
*/ |
|
818 |
public List<Runnable> shutdownNow() { |
|
819 |
return super.shutdownNow(); |
|
820 |
} |
|
821 |
||
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 | 836 |
* |
837 |
* @return the task queue |
|
838 |
*/ |
|
839 |
public BlockingQueue<Runnable> getQueue() { |
|
840 |
return super.getQueue(); |
|
841 |
} |
|
842 |
||
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 | 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 | 849 |
implements BlockingQueue<Runnable> { |
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 | 875 |
private RunnableScheduledFuture<?>[] queue = |
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 1077 |
} |
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 | 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 | 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 | 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 | 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 | 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 | 1110 |
} |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1111 |
|
11279 | 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 | 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 | 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 | 1124 |
first = null; // don't retain ref while waiting |
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 | 1144 |
} |
59
f35c86a7e82d
6602600: Fast removal of cancelled scheduled thread pool tasks
martin
parents:
2
diff
changeset
|
1145 |
|
11279 | 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 | 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 | 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 | 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 | 1186 |
} |
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 | 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 | 1210 |
// assert lock.isHeldByCurrentThread(); |
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 | 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 | 1235 |
} |
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 | 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 | 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 | 1318 |
} |
1319 |
} |
|
1320 |
} |