View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util.thread;
20  
21  import java.util.concurrent.ScheduledFuture;
22  import java.util.concurrent.ScheduledThreadPoolExecutor;
23  import java.util.concurrent.ThreadFactory;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.eclipse.jetty.util.component.AbstractLifeCycle;
27  
28  /**
29   * Implementation of {@link Scheduler} based on JDK's {@link ScheduledThreadPoolExecutor}.
30   * <p />
31   * While use of {@link ScheduledThreadPoolExecutor} creates futures that will not be used,
32   * it has the advantage of allowing to set a property to remove cancelled tasks from its
33   * queue even if the task did not fire, which provides a huge benefit in the performance
34   * of garbage collection in young generation.
35   */
36  public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Scheduler
37  {
38      private final String name;
39      private final boolean daemon;
40      private volatile ScheduledThreadPoolExecutor scheduler;
41  
42      public ScheduledExecutorScheduler()
43      {
44          this(null, false);
45      }
46  
47      public ScheduledExecutorScheduler(String name, boolean daemon)
48      {
49          this.name = name == null ? "Scheduler-" + hashCode() : name;
50          this.daemon = daemon;
51      }
52  
53      @Override
54      protected void doStart() throws Exception
55      {
56          scheduler = new ScheduledThreadPoolExecutor(1, new ThreadFactory()
57          {
58              @Override
59              public Thread newThread(Runnable r)
60              {
61                  Thread thread = new Thread(r, name);
62                  thread.setDaemon(daemon);
63                  return thread;
64              }
65          });
66          scheduler.setRemoveOnCancelPolicy(true);
67          super.doStart();
68      }
69  
70      @Override
71      protected void doStop() throws Exception
72      {
73          scheduler.shutdownNow();
74          super.doStop();
75          scheduler = null;
76      }
77  
78      @Override
79      public Task schedule(Runnable task, long delay, TimeUnit unit)
80      {
81          ScheduledFuture<?> result = scheduler.schedule(task, delay, unit);
82          return new ScheduledFutureTask(result);
83      }
84  
85      private class ScheduledFutureTask implements Task
86      {
87          private final ScheduledFuture<?> scheduledFuture;
88  
89          public ScheduledFutureTask(ScheduledFuture<?> scheduledFuture)
90          {
91              this.scheduledFuture = scheduledFuture;
92          }
93  
94          @Override
95          public boolean cancel()
96          {
97              return scheduledFuture.cancel(false);
98          }
99      }
100 }