View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.InterruptedIOException;
22  import java.io.PrintWriter;
23  import java.lang.Thread.UncaughtExceptionHandler;
24  import java.util.concurrent.LinkedBlockingQueue;
25  import java.util.concurrent.ThreadFactory;
26  import java.util.concurrent.ThreadPoolExecutor;
27  import java.util.concurrent.TimeUnit;
28  import java.util.concurrent.atomic.AtomicInteger;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.classification.InterfaceAudience;
33  import org.apache.hadoop.classification.InterfaceStability;
34  import org.apache.hadoop.util.ReflectionUtils;
35  
36  /**
37   * Thread Utility
38   */
39  @InterfaceAudience.Public
40  @InterfaceStability.Stable
41  public class Threads {
42    protected static final Log LOG = LogFactory.getLog(Threads.class);
43    private static final AtomicInteger poolNumber = new AtomicInteger(1);
44    /**
45     * Utility method that sets name, daemon status and starts passed thread.
46     * @param t thread to run
47     * @return Returns the passed Thread <code>t</code>.
48     */
49    public static Thread setDaemonThreadRunning(final Thread t) {
50      return setDaemonThreadRunning(t, t.getName());
51    }
52  
53    /**
54     * Utility method that sets name, daemon status and starts passed thread.
55     * @param t thread to frob
56     * @param name new name
57     * @return Returns the passed Thread <code>t</code>.
58     */
59    public static Thread setDaemonThreadRunning(final Thread t,
60      final String name) {
61      return setDaemonThreadRunning(t, name, null);
62    }
63  
64    /**
65     * Utility method that sets name, daemon status and starts passed thread.
66     * @param t thread to frob
67     * @param name new name
68     * @param handler A handler to set on the thread.  Pass null if want to
69     * use default handler.
70     * @return Returns the passed Thread <code>t</code>.
71     */
72    public static Thread setDaemonThreadRunning(final Thread t,
73      final String name, final UncaughtExceptionHandler handler) {
74      t.setName(name);
75      if (handler != null) {
76        t.setUncaughtExceptionHandler(handler);
77      }
78      t.setDaemon(true);
79      t.start();
80      return t;
81    }
82  
83    /**
84     * Shutdown passed thread using isAlive and join.
85     * @param t Thread to shutdown
86     */
87    public static void shutdown(final Thread t) {
88      shutdown(t, 0);
89    }
90  
91    /**
92     * Shutdown passed thread using isAlive and join.
93     * @param joinwait Pass 0 if we're to wait forever.
94     * @param t Thread to shutdown
95     */
96    public static void shutdown(final Thread t, final long joinwait) {
97      if (t == null) return;
98      while (t.isAlive()) {
99        try {
100         t.join(joinwait);
101       } catch (InterruptedException e) {
102         LOG.warn(t.getName() + "; joinwait=" + joinwait, e);
103       }
104     }
105   }
106 
107 
108   /**
109    * @param t Waits on the passed thread to die dumping a threaddump every
110    * minute while its up.
111    * @throws InterruptedException
112    */
113   public static void threadDumpingIsAlive(final Thread t)
114   throws InterruptedException {
115     if (t == null) {
116       return;
117     }
118 
119     while (t.isAlive()) {
120       t.join(60 * 1000);
121       if (t.isAlive()) {
122         ReflectionUtils.printThreadInfo(new PrintWriter(System.out),
123             "Automatic Stack Trace every 60 seconds waiting on " +
124             t.getName());
125       }
126     }
127   }
128 
129   /**
130    * If interrupted, just prints out the interrupt on STDOUT, resets interrupt and returns
131    * @param millis How long to sleep for in milliseconds.
132    */
133   public static void sleep(long millis) {
134     try {
135       Thread.sleep(millis);
136     } catch (InterruptedException e) {
137       e.printStackTrace();
138       Thread.currentThread().interrupt();
139     }
140   }
141 
142   /**
143    * Sleeps for the given amount of time even if interrupted. Preserves
144    * the interrupt status.
145    * @param msToWait the amount of time to sleep in milliseconds
146    */
147   public static void sleepWithoutInterrupt(final long msToWait) {
148     long timeMillis = System.currentTimeMillis();
149     long endTime = timeMillis + msToWait;
150     boolean interrupted = false;
151     while (timeMillis < endTime) {
152       try {
153         Thread.sleep(endTime - timeMillis);
154       } catch (InterruptedException ex) {
155         interrupted = true;
156       }
157       timeMillis = System.currentTimeMillis();
158     }
159 
160     if (interrupted) {
161       Thread.currentThread().interrupt();
162     }
163   }
164 
165   /**
166    * Create a new CachedThreadPool with a bounded number as the maximum 
167    * thread size in the pool.
168    * 
169    * @param maxCachedThread the maximum thread could be created in the pool
170    * @param timeout the maximum time to wait
171    * @param unit the time unit of the timeout argument
172    * @param threadFactory the factory to use when creating new threads
173    * @return threadPoolExecutor the cachedThreadPool with a bounded number 
174    * as the maximum thread size in the pool. 
175    */
176   public static ThreadPoolExecutor getBoundedCachedThreadPool(
177       int maxCachedThread, long timeout, TimeUnit unit,
178       ThreadFactory threadFactory) {
179     ThreadPoolExecutor boundedCachedThreadPool =
180       new ThreadPoolExecutor(maxCachedThread, maxCachedThread, timeout,
181         TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
182     // allow the core pool threads timeout and terminate
183     boundedCachedThreadPool.allowCoreThreadTimeOut(true);
184     return boundedCachedThreadPool;
185   }
186   
187   
188   /**
189    * Returns a {@link java.util.concurrent.ThreadFactory} that names each created thread uniquely,
190    * with a common prefix.
191    * @param prefix The prefix of every created Thread's name
192    * @return a {@link java.util.concurrent.ThreadFactory} that names threads
193    */
194   public static ThreadFactory getNamedThreadFactory(final String prefix) {
195     SecurityManager s = System.getSecurityManager();
196     final ThreadGroup threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread()
197         .getThreadGroup();
198 
199     return new ThreadFactory() {
200       final AtomicInteger threadNumber = new AtomicInteger(1);
201       private final int poolNumber = Threads.poolNumber.getAndIncrement();
202       final ThreadGroup group = threadGroup;
203 
204       @Override
205       public Thread newThread(Runnable r) {
206         final String name = prefix + "-pool-" + poolNumber + "-thread-"
207             + threadNumber.getAndIncrement();
208         return new Thread(group, r, name);
209       }
210     };
211   }
212 
213   /**
214    * Same as {#newDaemonThreadFactory(String, UncaughtExceptionHandler)},
215    * without setting the exception handler.
216    */
217   public static ThreadFactory newDaemonThreadFactory(final String prefix) {
218     return newDaemonThreadFactory(prefix, null);
219   }
220 
221   /**
222    * Get a named {@link ThreadFactory} that just builds daemon threads.
223    * @param prefix name prefix for all threads created from the factory
224    * @param handler unhandles exception handler to set for all threads
225    * @return a thread factory that creates named, daemon threads with
226    *         the supplied exception handler and normal priority
227    */
228   public static ThreadFactory newDaemonThreadFactory(final String prefix,
229       final UncaughtExceptionHandler handler) {
230     final ThreadFactory namedFactory = getNamedThreadFactory(prefix);
231     return new ThreadFactory() {
232       @Override
233       public Thread newThread(Runnable r) {
234         Thread t = namedFactory.newThread(r);
235         if (handler != null) {
236           t.setUncaughtExceptionHandler(handler);
237         }
238         if (!t.isDaemon()) {
239           t.setDaemon(true);
240         }
241         if (t.getPriority() != Thread.NORM_PRIORITY) {
242           t.setPriority(Thread.NORM_PRIORITY);
243         }
244         return t;
245       }
246 
247     };
248   }
249 }