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.BlockingQueue;
22  import java.util.concurrent.ScheduledFuture;
23  import java.util.concurrent.ScheduledThreadPoolExecutor;
24  import java.util.concurrent.ThreadFactory;
25  import java.util.concurrent.TimeUnit;
26  
27  import org.eclipse.jetty.util.component.AbstractLifeCycle;
28  
29  /**
30   * Implementation of {@link Scheduler} based on JDK's {@link ScheduledThreadPoolExecutor}.
31   * <p />
32   * While use of {@link ScheduledThreadPoolExecutor} creates futures that will not be used,
33   * it has the advantage of allowing to set a property to remove cancelled tasks from its
34   * queue even if the task did not fire, which provides a huge benefit in the performance
35   * of garbage collection in young generation.
36   */
37  public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Scheduler
38  {
39      private final String name;
40      private final boolean daemon;
41      private volatile ScheduledThreadPoolExecutor scheduler;
42      private ClassLoader classloader;
43  
44      public ScheduledExecutorScheduler()
45      {
46          this(null, false);
47      }  
48  
49      public ScheduledExecutorScheduler(String name, boolean daemon)
50      {
51          this (name,daemon, Thread.currentThread().getContextClassLoader());
52      }
53      
54      public ScheduledExecutorScheduler(String name, boolean daemon, ClassLoader threadFactoryClassLoader)
55      {
56          this.name = name == null ? "Scheduler-" + hashCode() : name;
57          this.daemon = daemon;
58          this.classloader = threadFactoryClassLoader;
59      }
60  
61      @Override
62      protected void doStart() throws Exception
63      {
64          scheduler = new ScheduledThreadPoolExecutor(1, new ThreadFactory()
65          {
66              @Override
67              public Thread newThread(Runnable r)
68              {
69                  Thread thread = new Thread(r, name);
70                  thread.setDaemon(daemon);
71                  thread.setContextClassLoader(classloader);
72                  return thread;
73              }
74          });
75          scheduler.setRemoveOnCancelPolicy(true);
76          super.doStart();
77      }
78  
79      
80  
81      @Override
82      protected void doStop() throws Exception
83      {
84          scheduler.shutdownNow();
85          super.doStop();
86          scheduler = null;
87      }
88  
89      @Override
90      public Task schedule(Runnable task, long delay, TimeUnit unit)
91      {
92          ScheduledFuture<?> result = scheduler.schedule(task, delay, unit);
93          return new ScheduledFutureTask(result);
94      }
95   
96  
97      private class ScheduledFutureTask implements Task
98      {
99          private final ScheduledFuture<?> scheduledFuture;
100 
101         public ScheduledFutureTask(ScheduledFuture<?> scheduledFuture)
102         {
103             this.scheduledFuture = scheduledFuture;
104         }
105 
106         @Override
107         public boolean cancel()
108         {
109             return scheduledFuture.cancel(false);
110         }
111     }
112 }