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 run
38 * @return Returns the passed Thread <code>t</code>.
39 */
40 public static Thread setDaemonThreadRunning(final Thread t) {
41 return setDaemonThreadRunning(t, t.getName());
42 }
43
44 /**
45 * Utility method that sets name, daemon status and starts passed thread.
46 * @param t thread to frob
47 * @param name new name
48 * @return Returns the passed Thread <code>t</code>.
49 */
50 public static Thread setDaemonThreadRunning(final Thread t,
51 final String name) {
52 return setDaemonThreadRunning(t, name, null);
53 }
54
55 /**
56 * Utility method that sets name, daemon status and starts passed thread.
57 * @param t thread to frob
58 * @param name new name
59 * @param handler A handler to set on the thread. Pass null if want to
60 * use default handler.
61 * @return Returns the passed Thread <code>t</code>.
62 */
63 public static Thread setDaemonThreadRunning(final Thread t,
64 final String name, final UncaughtExceptionHandler handler) {
65 t.setName(name);
66 if (handler != null) {
67 t.setUncaughtExceptionHandler(handler);
68 }
69 t.setDaemon(true);
70 t.start();
71 return t;
72 }
73
74 /**
75 * Shutdown passed thread using isAlive and join.
76 * @param t Thread to shutdown
77 */
78 public static void shutdown(final Thread t) {
79 shutdown(t, 0);
80 }
81
82 /**
83 * Shutdown passed thread using isAlive and join.
84 * @param joinwait Pass 0 if we're to wait forever.
85 * @param t Thread to shutdown
86 */
87 public static void shutdown(final Thread t, final long joinwait) {
88 if (t == null) return;
89 while (t.isAlive()) {
90 try {
91 t.join(joinwait);
92 } catch (InterruptedException e) {
93 LOG.warn(t.getName() + "; joinwait=" + joinwait, e);
94 }
95 }
96 }
97
98
99 /**
100 * @param t Waits on the passed thread to die dumping a threaddump every
101 * minute while its up.
102 * @throws InterruptedException
103 */
104 public static void threadDumpingIsAlive(final Thread t)
105 throws InterruptedException {
106 if (t == null) {
107 return;
108 }
109
110 while (t.isAlive()) {
111 t.join(60 * 1000);
112 if (t.isAlive()) {
113 ReflectionUtils.printThreadInfo(new PrintWriter(System.out),
114 "Automatic Stack Trace every 60 seconds waiting on " +
115 t.getName());
116 }
117 }
118 }
119
120 /**
121 * @param millis How long to sleep for in milliseconds.
122 */
123 public static void sleep(int millis) {
124 try {
125 Thread.sleep(millis);
126 } catch (InterruptedException e) {
127 e.printStackTrace();
128 }
129 }
130 }