View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.util;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import java.io.PrintWriter;
25  import org.apache.hadoop.util.ReflectionUtils;
26  
27  import java.lang.Thread.UncaughtExceptionHandler;
28  
29  /**
30   * Thread Utility
31   */
32  public class Threads {
33    protected static final Log LOG = LogFactory.getLog(Threads.class);
34  
35    /**
36     * Utility method that sets name, daemon status and starts passed thread.
37     * @param t thread to frob
38     * @param name new name
39     * @return Returns the passed Thread <code>t</code>.
40     */
41    public static Thread setDaemonThreadRunning(final Thread t,
42      final String name) {
43      return setDaemonThreadRunning(t, name, null);
44    }
45  
46    /**
47     * Utility method that sets name, daemon status and starts passed thread.
48     * @param t thread to frob
49     * @param name new name
50     * @param handler A handler to set on the thread.  Pass null if want to
51     * use default handler.
52     * @return Returns the passed Thread <code>t</code>.
53     */
54    public static Thread setDaemonThreadRunning(final Thread t,
55      final String name, final UncaughtExceptionHandler handler) {
56      t.setName(name);
57      if (handler != null) {
58        t.setUncaughtExceptionHandler(handler);
59      }
60      t.setDaemon(true);
61      t.start();
62      return t;
63    }
64  
65    /**
66     * Shutdown passed thread using isAlive and join.
67     * @param t Thread to shutdown
68     */
69    public static void shutdown(final Thread t) {
70      shutdown(t, 0);
71    }
72  
73    /**
74     * Shutdown passed thread using isAlive and join.
75     * @param joinwait Pass 0 if we're to wait forever.
76     * @param t Thread to shutdown
77     */
78    public static void shutdown(final Thread t, final long joinwait) {
79      if (t == null) return;
80      while (t.isAlive()) {
81        try {
82          t.join(joinwait);
83        } catch (InterruptedException e) {
84          LOG.warn(t.getName() + "; joinwait=" + joinwait, e);
85        }
86      }
87    }
88  
89  
90    /**
91     * @param t Waits on the passed thread to die dumping a threaddump every
92     * minute while its up.
93     * @throws InterruptedException
94     */
95    public static void threadDumpingIsAlive(final Thread t)
96    throws InterruptedException {
97      if (t == null) {
98        return;
99      }
100     long startTime = System.currentTimeMillis();
101     while (t.isAlive()) {
102       Thread.sleep(1000);
103       if (System.currentTimeMillis() - startTime > 60000) {
104         startTime = System.currentTimeMillis();
105         ReflectionUtils.printThreadInfo(new PrintWriter(System.out),
106             "Automatic Stack Trace every 60 seconds waiting on " +
107             t.getName());
108       }
109     }
110   }
111 
112   /**
113    * @param millis How long to sleep for in milliseconds.
114    */
115   public static void sleep(int millis) {
116     try {
117       Thread.sleep(millis);
118     } catch (InterruptedException e) {
119       e.printStackTrace();
120     }
121   }
122 }