Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java
          +++ new/src/share/classes/java/util/concurrent/ScheduledThreadPoolExecutor.java
↓ open down ↓ 54 lines elided ↑ open up ↑
  55   55   * <p>When a submitted task is cancelled before it is run, execution
  56   56   * is suppressed. By default, such a cancelled task is not
  57   57   * automatically removed from the work queue until its delay
  58   58   * elapses. While this enables further inspection and monitoring, it
  59   59   * may also cause unbounded retention of cancelled tasks. To avoid
  60   60   * this, set {@link #setRemoveOnCancelPolicy} to {@code true}, which
  61   61   * causes tasks to be immediately removed from the work queue at
  62   62   * time of cancellation.
  63   63   *
  64   64   * <p>Successive executions of a task scheduled via
  65      - * <code>scheduleAtFixedRate</code> or
  66      - * <code>scheduleWithFixedDelay</code> do not overlap. While different
       65 + * {@code scheduleAtFixedRate} or
       66 + * {@code scheduleWithFixedDelay} do not overlap. While different
  67   67   * executions may be performed by different threads, the effects of
  68   68   * prior executions <a
  69   69   * href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
  70   70   * those of subsequent ones.
  71   71   *
  72   72   * <p>While this class inherits from {@link ThreadPoolExecutor}, a few
  73   73   * of the inherited tuning methods are not useful for it. In
  74   74   * particular, because it acts as a fixed-sized pool using
  75   75   * {@code corePoolSize} threads and an unbounded queue, adjustments
  76   76   * to {@code maximumPoolSize} have no useful effect. Additionally, it
↓ open down ↓ 276 lines elided ↑ open up ↑
 353  353      /**
 354  354       * Cancels and clears the queue of all tasks that should not be run
 355  355       * due to shutdown policy.  Invoked within super.shutdown.
 356  356       */
 357  357      @Override void onShutdown() {
 358  358          BlockingQueue<Runnable> q = super.getQueue();
 359  359          boolean keepDelayed =
 360  360              getExecuteExistingDelayedTasksAfterShutdownPolicy();
 361  361          boolean keepPeriodic =
 362  362              getContinueExistingPeriodicTasksAfterShutdownPolicy();
 363      -        if (!keepDelayed && !keepPeriodic)
      363 +        if (!keepDelayed && !keepPeriodic) {
      364 +            for (Object e : q.toArray())
      365 +                if (e instanceof RunnableScheduledFuture<?>)
      366 +                    ((RunnableScheduledFuture<?>) e).cancel(false);
 364  367              q.clear();
      368 +        }
 365  369          else {
 366  370              // Traverse snapshot to avoid iterator exceptions
 367  371              for (Object e : q.toArray()) {
 368  372                  if (e instanceof RunnableScheduledFuture) {
 369  373                      RunnableScheduledFuture<?> t =
 370  374                          (RunnableScheduledFuture<?>)e;
 371  375                      if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed) ||
 372  376                          t.isCancelled()) { // also remove if already cancelled
 373  377                          if (q.remove(t))
 374  378                              t.cancel(false);
↓ open down ↓ 54 lines elided ↑ open up ↑
 429  433       * given initial parameters.
 430  434       *
 431  435       * @param corePoolSize the number of threads to keep in the pool, even
 432  436       *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
 433  437       * @param threadFactory the factory to use when the executor
 434  438       *        creates a new thread
 435  439       * @throws IllegalArgumentException if {@code corePoolSize < 0}
 436  440       * @throws NullPointerException if {@code threadFactory} is null
 437  441       */
 438  442      public ScheduledThreadPoolExecutor(int corePoolSize,
 439      -                             ThreadFactory threadFactory) {
      443 +                                       ThreadFactory threadFactory) {
 440  444          super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
 441  445                new DelayedWorkQueue(), threadFactory);
 442  446      }
 443  447  
 444  448      /**
 445  449       * Creates a new ScheduledThreadPoolExecutor with the given
 446  450       * initial parameters.
 447  451       *
 448  452       * @param corePoolSize the number of threads to keep in the pool, even
 449  453       *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
 450  454       * @param handler the handler to use when execution is blocked
 451  455       *        because the thread bounds and queue capacities are reached
 452  456       * @throws IllegalArgumentException if {@code corePoolSize < 0}
 453  457       * @throws NullPointerException if {@code handler} is null
 454  458       */
 455  459      public ScheduledThreadPoolExecutor(int corePoolSize,
 456      -                              RejectedExecutionHandler handler) {
      460 +                                       RejectedExecutionHandler handler) {
 457  461          super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
 458  462                new DelayedWorkQueue(), handler);
 459  463      }
 460  464  
 461  465      /**
 462  466       * Creates a new ScheduledThreadPoolExecutor with the given
 463  467       * initial parameters.
 464  468       *
 465  469       * @param corePoolSize the number of threads to keep in the pool, even
 466  470       *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
 467  471       * @param threadFactory the factory to use when the executor
 468  472       *        creates a new thread
 469  473       * @param handler the handler to use when execution is blocked
 470  474       *        because the thread bounds and queue capacities are reached
 471  475       * @throws IllegalArgumentException if {@code corePoolSize < 0}
 472  476       * @throws NullPointerException if {@code threadFactory} or
 473  477       *         {@code handler} is null
 474  478       */
 475  479      public ScheduledThreadPoolExecutor(int corePoolSize,
 476      -                              ThreadFactory threadFactory,
 477      -                              RejectedExecutionHandler handler) {
      480 +                                       ThreadFactory threadFactory,
      481 +                                       RejectedExecutionHandler handler) {
 478  482          super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
 479  483                new DelayedWorkQueue(), threadFactory, handler);
 480  484      }
 481  485  
 482  486      /**
 483  487       * Returns the trigger time of a delayed action.
 484  488       */
 485  489      private long triggerTime(long delay, TimeUnit unit) {
 486  490          return triggerTime(unit.toNanos((delay < 0) ? 0 : delay));
 487  491      }
↓ open down ↓ 786 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX