ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ScheduledThreadPoolExecutor.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ScheduledThreadPoolExecutor.java (file contents):
Revision 1.44 by jsr166, Sun Sep 16 21:35:10 2007 UTC vs.
Revision 1.45 by jsr166, Tue Sep 18 01:04:35 2007 UTC

# Line 764 | Line 764 | public class ScheduledThreadPoolExecutor
764          /**
765           * Set f's heapIndex if it is a ScheduledFutureTask.
766           */
767 <        private void setIndex(Object f, int idx) {
767 >        private void setIndex(RunnableScheduledFuture f, int idx) {
768              if (f instanceof ScheduledFutureTask)
769                  ((ScheduledFutureTask)f).heapIndex = idx;
770          }
# Line 843 | Line 843 | public class ScheduledThreadPoolExecutor
843           */
844          private int indexOf(Object x) {
845              if (x != null) {
846 <                for (int i = 0; i < size; i++)
847 <                    if (x.equals(queue[i]))
848 <                        return i;
846 >                if (x instanceof ScheduledFutureTask) {
847 >                    int i = ((ScheduledFutureTask) x).heapIndex;
848 >                    // Sanity check; x could conceivably be a
849 >                    // ScheduledFutureTask from some other pool.
850 >                    if (i >= 0 && i < size && queue[i] == x)
851 >                        return i;
852 >                } else {
853 >                    for (int i = 0; i < size; i++)
854 >                        if (x.equals(queue[i]))
855 >                            return i;
856 >                }
857              }
858              return -1;
859          }
860  
861 +        public boolean contains(Object x) {
862 +            final ReentrantLock lock = this.lock;
863 +            lock.lock();
864 +            try {
865 +                return indexOf(x) != -1;
866 +            } finally {
867 +                lock.unlock();
868 +            }
869 +        }
870 +
871          public boolean remove(Object x) {
854            boolean removed;
872              final ReentrantLock lock = this.lock;
873              lock.lock();
874              try {
875 <                int i;
876 <                if (x instanceof ScheduledFutureTask)
877 <                    i = ((ScheduledFutureTask)x).heapIndex;
878 <                else
879 <                    i = indexOf(x);
880 <                if (removed = (i >= 0 && i < size && queue[i] == x)) {
881 <                    setIndex(x, -1);
882 <                    int s = --size;
883 <                    RunnableScheduledFuture replacement = queue[s];
884 <                    queue[s] = null;
885 <                    if (s != i) {
886 <                        siftDown(i, replacement);
887 <                        if (queue[i] == replacement)
888 <                            siftUp(i, replacement);
872 <                    }
873 <                }
875 >                int i = indexOf(x);
876 >                if (i < 0)
877 >                    return false;
878 >
879 >                setIndex(queue[i], -1);
880 >                int s = --size;
881 >                RunnableScheduledFuture replacement = queue[s];
882 >                queue[s] = null;
883 >                if (s != i) {
884 >                    siftDown(i, replacement);
885 >                    if (queue[i] == replacement)
886 >                        siftUp(i, replacement);
887 >                }
888 >                return true;
889              } finally {
890                  lock.unlock();
891              }
877            return removed;
892          }
893  
894          public int size() {
881            int s;
895              final ReentrantLock lock = this.lock;
896              lock.lock();
897              try {
898 <                s = size;
898 >                return size;
899              } finally {
900                  lock.unlock();
901              }
889            return s;
902          }
903  
904          public boolean isEmpty() {
# Line 918 | Line 930 | public class ScheduledThreadPoolExecutor
930                  if (i >= queue.length)
931                      grow();
932                  size = i + 1;
921                boolean notify;
933                  if (i == 0) {
923                    notify = true;
934                      queue[0] = e;
935                      setIndex(e, 0);
936 <                }
927 <                else {
928 <                    notify = e.compareTo(queue[0]) < 0;
936 >                } else {
937                      siftUp(i, e);
938                  }
939 <                if (notify)
939 >                if (queue[0] == e)
940                      available.signalAll();
941              } finally {
942                  lock.unlock();
# Line 971 | Line 979 | public class ScheduledThreadPoolExecutor
979                      if (first == null)
980                          available.await();
981                      else {
982 <                        long delay =  first.getDelay(TimeUnit.NANOSECONDS);
982 >                        long delay = first.getDelay(TimeUnit.NANOSECONDS);
983                          if (delay > 0)
984                              available.awaitNanos(delay);
985                          else
# Line 1056 | Line 1064 | public class ScheduledThreadPoolExecutor
1064              final ReentrantLock lock = this.lock;
1065              lock.lock();
1066              try {
1067 +                RunnableScheduledFuture first;
1068                  int n = 0;
1069 <                for (;;) {
1070 <                    RunnableScheduledFuture first = pollExpired();
1071 <                    if (first != null) {
1072 <                        c.add(first);
1064 <                        ++n;
1065 <                    }
1066 <                    else
1067 <                        break;
1068 <                }
1069 <                if (n > 0)
1070 <                    available.signalAll();
1069 >                while ((first = pollExpired()) != null) {
1070 >                    c.add(first);
1071 >                    ++n;
1072 >                }
1073                  return n;
1074              } finally {
1075                  lock.unlock();
# Line 1084 | Line 1086 | public class ScheduledThreadPoolExecutor
1086              final ReentrantLock lock = this.lock;
1087              lock.lock();
1088              try {
1089 +                RunnableScheduledFuture first;
1090                  int n = 0;
1091 <                while (n < maxElements) {
1092 <                    RunnableScheduledFuture first = pollExpired();
1093 <                    if (first != null) {
1094 <                        c.add(first);
1092 <                        ++n;
1093 <                    }
1094 <                    else
1095 <                        break;
1096 <                }
1097 <                if (n > 0)
1098 <                    available.signalAll();
1091 >                while (n < maxElements && (first = pollExpired()) != null) {
1092 >                    c.add(first);
1093 >                    ++n;
1094 >                }
1095                  return n;
1096              } finally {
1097                  lock.unlock();
# Line 1106 | Line 1102 | public class ScheduledThreadPoolExecutor
1102              final ReentrantLock lock = this.lock;
1103              lock.lock();
1104              try {
1105 <                return Arrays.copyOf(queue, size);
1105 >                return Arrays.copyOf(queue, size, Object[].class);
1106              } finally {
1107                  lock.unlock();
1108              }
1109          }
1110  
1111 +        @SuppressWarnings("unchecked")
1112          public <T> T[] toArray(T[] a) {
1113              final ReentrantLock lock = this.lock;
1114              lock.lock();
# Line 1128 | Line 1125 | public class ScheduledThreadPoolExecutor
1125          }
1126  
1127          public Iterator<Runnable> iterator() {
1128 <            return new Itr(toArray());
1128 >            return new Itr(Arrays.copyOf(queue, size));
1129          }
1130  
1131          /**
1132           * Snapshot iterator that works off copy of underlying q array.
1133           */
1134          private class Itr implements Iterator<Runnable> {
1135 <            final Object[] array; // Array of all elements
1136 <            int cursor;           // index of next element to return;
1137 <            int lastRet;          // index of last element, or -1 if no such
1135 >            final RunnableScheduledFuture[] array;
1136 >            int cursor = 0;     // index of next element to return
1137 >            int lastRet = -1;   // index of last element, or -1 if no such
1138  
1139 <            Itr(Object[] array) {
1143 <                lastRet = -1;
1139 >            Itr(RunnableScheduledFuture[] array) {
1140                  this.array = array;
1141              }
1142  
# Line 1152 | Line 1148 | public class ScheduledThreadPoolExecutor
1148                  if (cursor >= array.length)
1149                      throw new NoSuchElementException();
1150                  lastRet = cursor;
1151 <                return (Runnable)array[cursor++];
1151 >                return array[cursor++];
1152              }
1153  
1154              public void remove() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines