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.61 by jsr166, Mon Jun 6 06:28:33 2011 UTC vs.
Revision 1.62 by jsr166, Mon Jun 27 21:01:06 2011 UTC

# Line 5 | Line 5
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   /**
# Line 202 | Line 204 | public class ScheduledThreadPoolExecutor
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) {
# Line 220 | Line 222 | public class ScheduledThreadPoolExecutor
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  
# Line 395 | Line 397 | public class ScheduledThreadPoolExecutor
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  
# Line 412 | Line 414 | public class ScheduledThreadPoolExecutor
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  
# Line 429 | Line 431 | public class ScheduledThreadPoolExecutor
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  
# Line 450 | Line 452 | public class ScheduledThreadPoolExecutor
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  
# Line 479 | Line 481 | public class ScheduledThreadPoolExecutor
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          }
# Line 587 | Line 589 | public class ScheduledThreadPoolExecutor
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
# Line 597 | Line 599 | public class ScheduledThreadPoolExecutor
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      /**
# Line 605 | Line 607 | public class ScheduledThreadPoolExecutor
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      /**
# Line 614 | Line 615 | public class ScheduledThreadPoolExecutor
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      /**
# Line 1031 | Line 1032 | public class ScheduledThreadPoolExecutor
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);
# Line 1049 | Line 1050 | public class ScheduledThreadPoolExecutor
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)
# Line 1087 | Line 1088 | public class ScheduledThreadPoolExecutor
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)
# Line 1132 | Line 1133 | public class ScheduledThreadPoolExecutor
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) {
# Line 1152 | Line 1153 | public class ScheduledThreadPoolExecutor
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;
# Line 1174 | Line 1176 | public class ScheduledThreadPoolExecutor
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines