5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
< |
import java.util.concurrent.atomic.*; |
9 |
< |
import java.util.concurrent.locks.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
9 |
> |
import java.util.concurrent.atomic.AtomicLong; |
10 |
> |
import java.util.concurrent.locks.Condition; |
11 |
> |
import java.util.concurrent.locks.ReentrantLock; |
12 |
|
import java.util.*; |
13 |
|
|
14 |
|
/** |
204 |
|
} |
205 |
|
|
206 |
|
public long getDelay(TimeUnit unit) { |
207 |
< |
return unit.convert(time - now(), TimeUnit.NANOSECONDS); |
207 |
> |
return unit.convert(time - now(), NANOSECONDS); |
208 |
|
} |
209 |
|
|
210 |
|
public int compareTo(Delayed other) { |
222 |
|
else |
223 |
|
return 1; |
224 |
|
} |
225 |
< |
long diff = (getDelay(TimeUnit.NANOSECONDS) - |
226 |
< |
other.getDelay(TimeUnit.NANOSECONDS)); |
225 |
> |
long diff = (getDelay(NANOSECONDS) - |
226 |
> |
other.getDelay(NANOSECONDS)); |
227 |
|
return (diff < 0) ? -1 : (diff > 0) ? 1 : 0; |
228 |
|
} |
229 |
|
|
397 |
|
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
398 |
|
*/ |
399 |
|
public ScheduledThreadPoolExecutor(int corePoolSize) { |
400 |
< |
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
400 |
> |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
401 |
|
new DelayedWorkQueue()); |
402 |
|
} |
403 |
|
|
414 |
|
*/ |
415 |
|
public ScheduledThreadPoolExecutor(int corePoolSize, |
416 |
|
ThreadFactory threadFactory) { |
417 |
< |
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
417 |
> |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
418 |
|
new DelayedWorkQueue(), threadFactory); |
419 |
|
} |
420 |
|
|
431 |
|
*/ |
432 |
|
public ScheduledThreadPoolExecutor(int corePoolSize, |
433 |
|
RejectedExecutionHandler handler) { |
434 |
< |
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
434 |
> |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
435 |
|
new DelayedWorkQueue(), handler); |
436 |
|
} |
437 |
|
|
452 |
|
public ScheduledThreadPoolExecutor(int corePoolSize, |
453 |
|
ThreadFactory threadFactory, |
454 |
|
RejectedExecutionHandler handler) { |
455 |
< |
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
455 |
> |
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, |
456 |
|
new DelayedWorkQueue(), threadFactory, handler); |
457 |
|
} |
458 |
|
|
481 |
|
private long overflowFree(long delay) { |
482 |
|
Delayed head = (Delayed) super.getQueue().peek(); |
483 |
|
if (head != null) { |
484 |
< |
long headDelay = head.getDelay(TimeUnit.NANOSECONDS); |
484 |
> |
long headDelay = head.getDelay(NANOSECONDS); |
485 |
|
if (headDelay < 0 && (delay - headDelay < 0)) |
486 |
|
delay = Long.MAX_VALUE + headDelay; |
487 |
|
} |
589 |
|
* @throws NullPointerException {@inheritDoc} |
590 |
|
*/ |
591 |
|
public void execute(Runnable command) { |
592 |
< |
schedule(command, 0, TimeUnit.NANOSECONDS); |
592 |
> |
schedule(command, 0, NANOSECONDS); |
593 |
|
} |
594 |
|
|
595 |
|
// Override AbstractExecutorService methods |
599 |
|
* @throws NullPointerException {@inheritDoc} |
600 |
|
*/ |
601 |
|
public Future<?> submit(Runnable task) { |
602 |
< |
return schedule(task, 0, TimeUnit.NANOSECONDS); |
602 |
> |
return schedule(task, 0, NANOSECONDS); |
603 |
|
} |
604 |
|
|
605 |
|
/** |
607 |
|
* @throws NullPointerException {@inheritDoc} |
608 |
|
*/ |
609 |
|
public <T> Future<T> submit(Runnable task, T result) { |
610 |
< |
return schedule(Executors.callable(task, result), |
609 |
< |
0, TimeUnit.NANOSECONDS); |
610 |
> |
return schedule(Executors.callable(task, result), 0, NANOSECONDS); |
611 |
|
} |
612 |
|
|
613 |
|
/** |
615 |
|
* @throws NullPointerException {@inheritDoc} |
616 |
|
*/ |
617 |
|
public <T> Future<T> submit(Callable<T> task) { |
618 |
< |
return schedule(task, 0, TimeUnit.NANOSECONDS); |
618 |
> |
return schedule(task, 0, NANOSECONDS); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
1032 |
|
lock.lock(); |
1033 |
|
try { |
1034 |
|
RunnableScheduledFuture<?> first = queue[0]; |
1035 |
< |
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
1035 |
> |
if (first == null || first.getDelay(NANOSECONDS) > 0) |
1036 |
|
return null; |
1037 |
|
else |
1038 |
|
return finishPoll(first); |
1050 |
|
if (first == null) |
1051 |
|
available.await(); |
1052 |
|
else { |
1053 |
< |
long delay = first.getDelay(TimeUnit.NANOSECONDS); |
1053 |
> |
long delay = first.getDelay(NANOSECONDS); |
1054 |
|
if (delay <= 0) |
1055 |
|
return finishPoll(first); |
1056 |
|
else if (leader != null) |
1088 |
|
else |
1089 |
|
nanos = available.awaitNanos(nanos); |
1090 |
|
} else { |
1091 |
< |
long delay = first.getDelay(TimeUnit.NANOSECONDS); |
1091 |
> |
long delay = first.getDelay(NANOSECONDS); |
1092 |
|
if (delay <= 0) |
1093 |
|
return finishPoll(first); |
1094 |
|
if (nanos <= 0) |
1133 |
|
} |
1134 |
|
|
1135 |
|
/** |
1136 |
< |
* Return and remove first element only if it is expired. |
1136 |
> |
* Return first element only if it is expired. |
1137 |
|
* Used only by drainTo. Call only when holding lock. |
1138 |
|
*/ |
1139 |
< |
private RunnableScheduledFuture<?> pollExpired() { |
1139 |
> |
private RunnableScheduledFuture<?> peekExpired() { |
1140 |
> |
// assert lock.isHeldByCurrentThread(); |
1141 |
|
RunnableScheduledFuture<?> first = queue[0]; |
1142 |
< |
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
1143 |
< |
return null; |
1142 |
< |
return finishPoll(first); |
1142 |
> |
return (first == null || first.getDelay(NANOSECONDS) > 0) ? |
1143 |
> |
null : first; |
1144 |
|
} |
1145 |
|
|
1146 |
|
public int drainTo(Collection<? super Runnable> c) { |
1153 |
|
try { |
1154 |
|
RunnableScheduledFuture<?> first; |
1155 |
|
int n = 0; |
1156 |
< |
while ((first = pollExpired()) != null) { |
1157 |
< |
c.add(first); |
1156 |
> |
while ((first = peekExpired()) != null) { |
1157 |
> |
c.add(first); // In this order, in case add() throws. |
1158 |
> |
finishPoll(first); |
1159 |
|
++n; |
1160 |
|
} |
1161 |
|
return n; |
1176 |
|
try { |
1177 |
|
RunnableScheduledFuture<?> first; |
1178 |
|
int n = 0; |
1179 |
< |
while (n < maxElements && (first = pollExpired()) != null) { |
1180 |
< |
c.add(first); |
1179 |
> |
while (n < maxElements && (first = peekExpired()) != null) { |
1180 |
> |
c.add(first); // In this order, in case add() throws. |
1181 |
> |
finishPoll(first); |
1182 |
|
++n; |
1183 |
|
} |
1184 |
|
return n; |