1178 |
|
} |
1179 |
|
} |
1180 |
|
|
1181 |
– |
/** |
1182 |
– |
* Returns first element only if it is expired. |
1183 |
– |
* Used only by drainTo. Call only when holding lock. |
1184 |
– |
*/ |
1185 |
– |
private RunnableScheduledFuture<?> peekExpired() { |
1186 |
– |
// assert lock.isHeldByCurrentThread(); |
1187 |
– |
RunnableScheduledFuture<?> first = queue[0]; |
1188 |
– |
return (first == null || first.getDelay(NANOSECONDS) > 0) ? |
1189 |
– |
null : first; |
1190 |
– |
} |
1191 |
– |
|
1181 |
|
public int drainTo(Collection<? super Runnable> c) { |
1182 |
< |
if (c == null) |
1194 |
< |
throw new NullPointerException(); |
1195 |
< |
if (c == this) |
1196 |
< |
throw new IllegalArgumentException(); |
1197 |
< |
final ReentrantLock lock = this.lock; |
1198 |
< |
lock.lock(); |
1199 |
< |
try { |
1200 |
< |
RunnableScheduledFuture<?> first; |
1201 |
< |
int n = 0; |
1202 |
< |
while ((first = peekExpired()) != null) { |
1203 |
< |
c.add(first); // In this order, in case add() throws. |
1204 |
< |
finishPoll(first); |
1205 |
< |
++n; |
1206 |
< |
} |
1207 |
< |
return n; |
1208 |
< |
} finally { |
1209 |
< |
lock.unlock(); |
1210 |
< |
} |
1182 |
> |
return drainTo(c, Integer.MAX_VALUE); |
1183 |
|
} |
1184 |
|
|
1185 |
|
public int drainTo(Collection<? super Runnable> c, int maxElements) { |
1194 |
|
try { |
1195 |
|
RunnableScheduledFuture<?> first; |
1196 |
|
int n = 0; |
1197 |
< |
while (n < maxElements && (first = peekExpired()) != null) { |
1197 |
> |
while (n < maxElements |
1198 |
> |
&& (first = queue[0]) != null |
1199 |
> |
&& first.getDelay(NANOSECONDS) <= 0) { |
1200 |
|
c.add(first); // In this order, in case add() throws. |
1201 |
|
finishPoll(first); |
1202 |
|
++n; |