220 |
|
else |
221 |
|
return 1; |
222 |
|
} |
223 |
< |
long d = (getDelay(TimeUnit.NANOSECONDS) - |
224 |
< |
other.getDelay(TimeUnit.NANOSECONDS)); |
225 |
< |
return (d == 0) ? 0 : ((d < 0) ? -1 : 1); |
223 |
> |
long diff = (getDelay(TimeUnit.NANOSECONDS) - |
224 |
> |
other.getDelay(TimeUnit.NANOSECONDS)); |
225 |
> |
return (diff < 0) ? -1 : (diff > 0) ? 1 : 0; |
226 |
|
} |
227 |
|
|
228 |
|
/** |
802 |
|
*/ |
803 |
|
|
804 |
|
private static final int INITIAL_CAPACITY = 16; |
805 |
< |
private RunnableScheduledFuture[] queue = |
806 |
< |
new RunnableScheduledFuture[INITIAL_CAPACITY]; |
805 |
> |
private RunnableScheduledFuture<?>[] queue = |
806 |
> |
new RunnableScheduledFuture<?>[INITIAL_CAPACITY]; |
807 |
|
private final ReentrantLock lock = new ReentrantLock(); |
808 |
|
private int size = 0; |
809 |
|
|
834 |
|
/** |
835 |
|
* Set f's heapIndex if it is a ScheduledFutureTask. |
836 |
|
*/ |
837 |
< |
private void setIndex(RunnableScheduledFuture f, int idx) { |
837 |
> |
private void setIndex(RunnableScheduledFuture<?> f, int idx) { |
838 |
|
if (f instanceof ScheduledFutureTask) |
839 |
|
((ScheduledFutureTask)f).heapIndex = idx; |
840 |
|
} |
843 |
|
* Sift element added at bottom up to its heap-ordered spot. |
844 |
|
* Call only when holding lock. |
845 |
|
*/ |
846 |
< |
private void siftUp(int k, RunnableScheduledFuture key) { |
846 |
> |
private void siftUp(int k, RunnableScheduledFuture<?> key) { |
847 |
|
while (k > 0) { |
848 |
|
int parent = (k - 1) >>> 1; |
849 |
< |
RunnableScheduledFuture e = queue[parent]; |
849 |
> |
RunnableScheduledFuture<?> e = queue[parent]; |
850 |
|
if (key.compareTo(e) >= 0) |
851 |
|
break; |
852 |
|
queue[k] = e; |
861 |
|
* Sift element added at top down to its heap-ordered spot. |
862 |
|
* Call only when holding lock. |
863 |
|
*/ |
864 |
< |
private void siftDown(int k, RunnableScheduledFuture key) { |
864 |
> |
private void siftDown(int k, RunnableScheduledFuture<?> key) { |
865 |
|
int half = size >>> 1; |
866 |
|
while (k < half) { |
867 |
|
int child = (k << 1) + 1; |
868 |
< |
RunnableScheduledFuture c = queue[child]; |
868 |
> |
RunnableScheduledFuture<?> c = queue[child]; |
869 |
|
int right = child + 1; |
870 |
|
if (right < size && c.compareTo(queue[right]) > 0) |
871 |
|
c = queue[child = right]; |
930 |
|
|
931 |
|
setIndex(queue[i], -1); |
932 |
|
int s = --size; |
933 |
< |
RunnableScheduledFuture replacement = queue[s]; |
933 |
> |
RunnableScheduledFuture<?> replacement = queue[s]; |
934 |
|
queue[s] = null; |
935 |
|
if (s != i) { |
936 |
|
siftDown(i, replacement); |
961 |
|
return Integer.MAX_VALUE; |
962 |
|
} |
963 |
|
|
964 |
< |
public RunnableScheduledFuture peek() { |
964 |
> |
public RunnableScheduledFuture<?> peek() { |
965 |
|
final ReentrantLock lock = this.lock; |
966 |
|
lock.lock(); |
967 |
|
try { |
974 |
|
public boolean offer(Runnable x) { |
975 |
|
if (x == null) |
976 |
|
throw new NullPointerException(); |
977 |
< |
RunnableScheduledFuture e = (RunnableScheduledFuture)x; |
977 |
> |
RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>)x; |
978 |
|
final ReentrantLock lock = this.lock; |
979 |
|
lock.lock(); |
980 |
|
try { |
1016 |
|
* holding lock. |
1017 |
|
* @param f the task to remove and return |
1018 |
|
*/ |
1019 |
< |
private RunnableScheduledFuture finishPoll(RunnableScheduledFuture f) { |
1019 |
> |
private RunnableScheduledFuture<?> finishPoll(RunnableScheduledFuture<?> f) { |
1020 |
|
int s = --size; |
1021 |
< |
RunnableScheduledFuture x = queue[s]; |
1021 |
> |
RunnableScheduledFuture<?> x = queue[s]; |
1022 |
|
queue[s] = null; |
1023 |
|
if (s != 0) |
1024 |
|
siftDown(0, x); |
1026 |
|
return f; |
1027 |
|
} |
1028 |
|
|
1029 |
< |
public RunnableScheduledFuture poll() { |
1029 |
> |
public RunnableScheduledFuture<?> poll() { |
1030 |
|
final ReentrantLock lock = this.lock; |
1031 |
|
lock.lock(); |
1032 |
|
try { |
1033 |
< |
RunnableScheduledFuture first = queue[0]; |
1033 |
> |
RunnableScheduledFuture<?> first = queue[0]; |
1034 |
|
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
1035 |
|
return null; |
1036 |
|
else |
1040 |
|
} |
1041 |
|
} |
1042 |
|
|
1043 |
< |
public RunnableScheduledFuture take() throws InterruptedException { |
1043 |
> |
public RunnableScheduledFuture<?> take() throws InterruptedException { |
1044 |
|
final ReentrantLock lock = this.lock; |
1045 |
|
lock.lockInterruptibly(); |
1046 |
|
try { |
1047 |
|
for (;;) { |
1048 |
< |
RunnableScheduledFuture first = queue[0]; |
1048 |
> |
RunnableScheduledFuture<?> first = queue[0]; |
1049 |
|
if (first == null) |
1050 |
|
available.await(); |
1051 |
|
else { |
1073 |
|
} |
1074 |
|
} |
1075 |
|
|
1076 |
< |
public RunnableScheduledFuture poll(long timeout, TimeUnit unit) |
1076 |
> |
public RunnableScheduledFuture<?> poll(long timeout, TimeUnit unit) |
1077 |
|
throws InterruptedException { |
1078 |
|
long nanos = unit.toNanos(timeout); |
1079 |
|
final ReentrantLock lock = this.lock; |
1080 |
|
lock.lockInterruptibly(); |
1081 |
|
try { |
1082 |
|
for (;;) { |
1083 |
< |
RunnableScheduledFuture first = queue[0]; |
1083 |
> |
RunnableScheduledFuture<?> first = queue[0]; |
1084 |
|
if (first == null) { |
1085 |
|
if (nanos <= 0) |
1086 |
|
return null; |
1119 |
|
lock.lock(); |
1120 |
|
try { |
1121 |
|
for (int i = 0; i < size; i++) { |
1122 |
< |
RunnableScheduledFuture t = queue[i]; |
1122 |
> |
RunnableScheduledFuture<?> t = queue[i]; |
1123 |
|
if (t != null) { |
1124 |
|
queue[i] = null; |
1125 |
|
setIndex(t, -1); |
1135 |
|
* Return and remove first element only if it is expired. |
1136 |
|
* Used only by drainTo. Call only when holding lock. |
1137 |
|
*/ |
1138 |
< |
private RunnableScheduledFuture pollExpired() { |
1139 |
< |
RunnableScheduledFuture first = queue[0]; |
1138 |
> |
private RunnableScheduledFuture<?> pollExpired() { |
1139 |
> |
RunnableScheduledFuture<?> first = queue[0]; |
1140 |
|
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) |
1141 |
|
return null; |
1142 |
|
return finishPoll(first); |
1150 |
|
final ReentrantLock lock = this.lock; |
1151 |
|
lock.lock(); |
1152 |
|
try { |
1153 |
< |
RunnableScheduledFuture first; |
1153 |
> |
RunnableScheduledFuture<?> first; |
1154 |
|
int n = 0; |
1155 |
|
while ((first = pollExpired()) != null) { |
1156 |
|
c.add(first); |
1172 |
|
final ReentrantLock lock = this.lock; |
1173 |
|
lock.lock(); |
1174 |
|
try { |
1175 |
< |
RunnableScheduledFuture first; |
1175 |
> |
RunnableScheduledFuture<?> first; |
1176 |
|
int n = 0; |
1177 |
|
while (n < maxElements && (first = pollExpired()) != null) { |
1178 |
|
c.add(first); |