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.20 by dl, Sat Apr 10 14:24:36 2004 UTC vs.
Revision 1.21 by jsr166, Tue Apr 26 01:17:18 2005 UTC

# Line 31 | Line 31 | import java.util.*;
31   * @since 1.5
32   * @author Doug Lea
33   */
34 < public class ScheduledThreadPoolExecutor
35 <        extends ThreadPoolExecutor
34 > public class ScheduledThreadPoolExecutor
35 >        extends ThreadPoolExecutor
36          implements ScheduledExecutorService {
37  
38      /**
# Line 61 | Line 61 | public class ScheduledThreadPoolExecutor
61          return System.nanoTime() - NANO_ORIGIN;
62      }
63  
64 <    private class ScheduledFutureTask<V>
64 >    private class ScheduledFutureTask<V>
65              extends FutureTask<V> implements ScheduledFuture<V> {
66 <        
66 >
67          /** Sequence number to break ties FIFO */
68          private final long sequenceNumber;
69          /** The time the task is enabled to execute in nanoTime units */
# Line 142 | Line 142 | public class ScheduledThreadPoolExecutor
142              boolean down = isShutdown();
143              // Reschedule if not cancelled and not shutdown or policy allows
144              if (ok && (!down ||
145 <                       (getContinueExistingPeriodicTasksAfterShutdownPolicy() &&
145 >                       (getContinueExistingPeriodicTasksAfterShutdownPolicy() &&
146                          !isTerminating()))) {
147                  long p = period;
148                  if (p > 0)
# Line 153 | Line 153 | public class ScheduledThreadPoolExecutor
153              }
154              // This might have been the final executed delayed
155              // task.  Wake up threads to check.
156 <            else if (down)
156 >            else if (down)
157                  interruptIdleWorkers();
158          }
159  
160          /**
161           * Overrides FutureTask version so as to reset/requeue if periodic.
162 <         */
162 >         */
163          public void run() {
164              if (isPeriodic())
165                  runPeriodic();
166 <            else
166 >            else
167                  ScheduledFutureTask.super.run();
168          }
169      }
# Line 181 | Line 181 | public class ScheduledThreadPoolExecutor
181          // run yet, so thread will just idle until delay elapses.
182          if (getPoolSize() < getCorePoolSize())
183              prestartCoreThread();
184 <            
184 >
185          super.getQueue().add(command);
186      }
187  
188      /**
189 <     * Cancel and clear the queue of all tasks that should not be run
189 >     * Cancels and clears the queue of all tasks that should not be run
190       * due to shutdown policy.
191       */
192      private void cancelUnwantedTasks() {
193          boolean keepDelayed = getExecuteExistingDelayedTasksAfterShutdownPolicy();
194          boolean keepPeriodic = getContinueExistingPeriodicTasksAfterShutdownPolicy();
195 <        if (!keepDelayed && !keepPeriodic)
195 >        if (!keepDelayed && !keepPeriodic)
196              super.getQueue().clear();
197          else if (keepDelayed || keepPeriodic) {
198              Object[] entries = super.getQueue().toArray();
# Line 210 | Line 210 | public class ScheduledThreadPoolExecutor
210      }
211  
212      public boolean remove(Runnable task) {
213 <        if (!(task instanceof ScheduledFutureTask))
214 <            return false;
215 <        return getQueue().remove(task);
213 >        if (!(task instanceof ScheduledFutureTask))
214 >            return false;
215 >        return getQueue().remove(task);
216      }
217  
218      /**
219       * Creates a new ScheduledThreadPoolExecutor with the given core
220       * pool size.
221 <     *
221 >     *
222       * @param corePoolSize the number of threads to keep in the pool,
223       * even if they are idle.
224       * @throws IllegalArgumentException if corePoolSize less than or
# Line 232 | Line 232 | public class ScheduledThreadPoolExecutor
232      /**
233       * Creates a new ScheduledThreadPoolExecutor with the given
234       * initial parameters.
235 <     *
235 >     *
236       * @param corePoolSize the number of threads to keep in the pool,
237       * even if they are idle.
238       * @param threadFactory the factory to use when the executor
239 <     * creates a new thread.
239 >     * creates a new thread.
240       * @throws NullPointerException if threadFactory is null
241       */
242      public ScheduledThreadPoolExecutor(int corePoolSize,
# Line 248 | Line 248 | public class ScheduledThreadPoolExecutor
248      /**
249       * Creates a new ScheduledThreadPoolExecutor with the given
250       * initial parameters.
251 <     *
251 >     *
252       * @param corePoolSize the number of threads to keep in the pool,
253       * even if they are idle.
254       * @param handler the handler to use when execution is blocked
# Line 264 | Line 264 | public class ScheduledThreadPoolExecutor
264      /**
265       * Creates a new ScheduledThreadPoolExecutor with the given
266       * initial parameters.
267 <     *
267 >     *
268       * @param corePoolSize the number of threads to keep in the pool,
269       * even if they are idle.
270       * @param threadFactory the factory to use when the executor
271 <     * creates a new thread.
271 >     * creates a new thread.
272       * @param handler the handler to use when execution is blocked
273       * because the thread bounds and queue capacities are reached.
274       * @throws NullPointerException if threadFactory or handler is null
# Line 280 | Line 280 | public class ScheduledThreadPoolExecutor
280                new DelayedWorkQueue(), threadFactory, handler);
281      }
282  
283 <    public ScheduledFuture<?> schedule(Runnable command,
284 <                                       long delay,
283 >    public ScheduledFuture<?> schedule(Runnable command,
284 >                                       long delay,
285                                         TimeUnit unit) {
286          if (command == null || unit == null)
287              throw new NullPointerException();
288          long triggerTime = now() + unit.toNanos(delay);
289 <        ScheduledFutureTask<?> t =
289 >        ScheduledFutureTask<?> t =
290              new ScheduledFutureTask<Boolean>(command, null, triggerTime);
291          delayedExecute(t);
292          return t;
293      }
294  
295 <    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
296 <                                           long delay,
295 >    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
296 >                                           long delay,
297                                             TimeUnit unit) {
298          if (callable == null || unit == null)
299              throw new NullPointerException();
300          if (delay < 0) delay = 0;
301          long triggerTime = now() + unit.toNanos(delay);
302 <        ScheduledFutureTask<V> t =
302 >        ScheduledFutureTask<V> t =
303              new ScheduledFutureTask<V>(callable, triggerTime);
304          delayedExecute(t);
305          return t;
306      }
307  
308 <    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
309 <                                                  long initialDelay,  
310 <                                                  long period,
308 >    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
309 >                                                  long initialDelay,
310 >                                                  long period,
311                                                    TimeUnit unit) {
312          if (command == null || unit == null)
313              throw new NullPointerException();
# Line 315 | Line 315 | public class ScheduledThreadPoolExecutor
315              throw new IllegalArgumentException();
316          if (initialDelay < 0) initialDelay = 0;
317          long triggerTime = now() + unit.toNanos(initialDelay);
318 <        ScheduledFutureTask<?> t =
319 <            new ScheduledFutureTask<Object>(command,
318 >        ScheduledFutureTask<?> t =
319 >            new ScheduledFutureTask<Object>(command,
320                                              null,
321                                              triggerTime,
322                                              unit.toNanos(period));
323          delayedExecute(t);
324          return t;
325      }
326 <    
327 <    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
328 <                                                     long initialDelay,  
329 <                                                     long delay,
326 >
327 >    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
328 >                                                     long initialDelay,
329 >                                                     long delay,
330                                                       TimeUnit unit) {
331          if (command == null || unit == null)
332              throw new NullPointerException();
# Line 334 | Line 334 | public class ScheduledThreadPoolExecutor
334              throw new IllegalArgumentException();
335          if (initialDelay < 0) initialDelay = 0;
336          long triggerTime = now() + unit.toNanos(initialDelay);
337 <        ScheduledFutureTask<?> t =
338 <            new ScheduledFutureTask<Boolean>(command,
337 >        ScheduledFutureTask<?> t =
338 >            new ScheduledFutureTask<Boolean>(command,
339                                               null,
340                                               triggerTime,
341                                               unit.toNanos(-delay));
342          delayedExecute(t);
343          return t;
344      }
345 <    
345 >
346  
347      /**
348 <     * Execute command with zero required delay. This has effect
348 >     * Executes command with zero required delay. This has effect
349       * equivalent to <tt>schedule(command, 0, anyUnit)</tt>.  Note
350       * that inspections of the queue and of the list returned by
351       * <tt>shutdownNow</tt> will access the zero-delayed
# Line 370 | Line 370 | public class ScheduledThreadPoolExecutor
370      }
371  
372      public <T> Future<T> submit(Runnable task, T result) {
373 <        return schedule(Executors.callable(task, result),
373 >        return schedule(Executors.callable(task, result),
374                          0, TimeUnit.NANOSECONDS);
375      }
376  
# Line 379 | Line 379 | public class ScheduledThreadPoolExecutor
379      }
380  
381      /**
382 <     * Set policy on whether to continue executing existing periodic
382 >     * Sets the policy on whether to continue executing existing periodic
383       * tasks even when this executor has been <tt>shutdown</tt>. In
384       * this case, these tasks will only terminate upon
385       * <tt>shutdownNow</tt>, or after setting the policy to
# Line 395 | Line 395 | public class ScheduledThreadPoolExecutor
395      }
396  
397      /**
398 <     * Get the policy on whether to continue executing existing
398 >     * Gets the policy on whether to continue executing existing
399       * periodic tasks even when this executor has been
400       * <tt>shutdown</tt>. In this case, these tasks will only
401       * terminate upon <tt>shutdownNow</tt> or after setting the policy
# Line 409 | Line 409 | public class ScheduledThreadPoolExecutor
409      }
410  
411      /**
412 <     * Set policy on whether to execute existing delayed
412 >     * Sets the policy on whether to execute existing delayed
413       * tasks even when this executor has been <tt>shutdown</tt>. In
414       * this case, these tasks will only terminate upon
415       * <tt>shutdownNow</tt>, or after setting the policy to
# Line 425 | Line 425 | public class ScheduledThreadPoolExecutor
425      }
426  
427      /**
428 <     * Get policy on whether to execute existing delayed
428 >     * Gets the policy on whether to execute existing delayed
429       * tasks even when this executor has been <tt>shutdown</tt>. In
430       * this case, these tasks will only terminate upon
431       * <tt>shutdownNow</tt>, or after setting the policy to
# Line 457 | Line 457 | public class ScheduledThreadPoolExecutor
457      /**
458       * Attempts to stop all actively executing tasks, halts the
459       * processing of waiting tasks, and returns a list of the tasks that were
460 <     * awaiting execution.
461 <     *  
460 >     * awaiting execution.
461 >     *
462       * <p>There are no guarantees beyond best-effort attempts to stop
463       * processing actively executing tasks.  This implementation
464       * cancels tasks via {@link Thread#interrupt}, so if any tasks mask or
# Line 492 | Line 492 | public class ScheduledThreadPoolExecutor
492      /**
493       * An annoying wrapper class to convince generics compiler to
494       * use a DelayQueue<ScheduledFutureTask> as a BlockingQueue<Runnable>
495 <     */
496 <    private static class DelayedWorkQueue
497 <        extends AbstractCollection<Runnable>
495 >     */
496 >    private static class DelayedWorkQueue
497 >        extends AbstractCollection<Runnable>
498          implements BlockingQueue<Runnable> {
499 <        
499 >
500          private final DelayQueue<ScheduledFutureTask> dq = new DelayQueue<ScheduledFutureTask>();
501          public Runnable poll() { return dq.poll(); }
502          public Runnable peek() { return dq.peek(); }
# Line 508 | Line 508 | public class ScheduledThreadPoolExecutor
508          public boolean add(Runnable x) { return dq.add((ScheduledFutureTask)x); }
509          public boolean offer(Runnable x) { return dq.offer((ScheduledFutureTask)x); }
510          public void put(Runnable x)  {
511 <            dq.put((ScheduledFutureTask)x);
511 >            dq.put((ScheduledFutureTask)x);
512          }
513          public boolean offer(Runnable x, long timeout, TimeUnit unit) {
514              return dq.offer((ScheduledFutureTask)x, timeout, unit);
# Line 518 | Line 518 | public class ScheduledThreadPoolExecutor
518          public Runnable element() { return dq.element(); }
519          public void clear() { dq.clear(); }
520          public int drainTo(Collection<? super Runnable> c) { return dq.drainTo(c); }
521 <        public int drainTo(Collection<? super Runnable> c, int maxElements) {
522 <            return dq.drainTo(c, maxElements);
521 >        public int drainTo(Collection<? super Runnable> c, int maxElements) {
522 >            return dq.drainTo(c, maxElements);
523          }
524  
525          public int remainingCapacity() { return dq.remainingCapacity(); }
# Line 529 | Line 529 | public class ScheduledThreadPoolExecutor
529          public boolean isEmpty() { return dq.isEmpty(); }
530          public Object[] toArray() { return dq.toArray(); }
531          public <T> T[] toArray(T[] array) { return dq.toArray(array); }
532 <        public Iterator<Runnable> iterator() {
532 >        public Iterator<Runnable> iterator() {
533              return new Iterator<Runnable>() {
534                  private Iterator<ScheduledFutureTask> it = dq.iterator();
535                  public boolean hasNext() { return it.hasNext(); }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines