-
Notifications
You must be signed in to change notification settings - Fork 14
/
MyAppThread.java
79 lines (61 loc) · 2.18 KB
/
MyAppThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.concurrency.thread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 定制 Thread 基类,包括 为线程指定名字,设置自定义 UncaughtExceptionHandler 向 Logger 中写入信息,
* 维护一些统计信息(包括有多少个线程被创建和销毁),以及在线程被创建或者终止时把调试消息写入日志。
*
* @author xingle
* @since 2016年06月25日 22:12
*/
public class MyAppThread extends Thread {
private static final Logger logger = LoggerFactory.getLogger(MyAppThread.class);
private static final String DEFAULT_NAME = "MyAppThread";
private static volatile boolean debugLifecycle = false;
private static final AtomicInteger created = new AtomicInteger(0);
private static final AtomicInteger alive = new AtomicInteger(0);
public MyAppThread(Runnable runnable) {
this(runnable, DEFAULT_NAME);
}
public MyAppThread(Runnable runnable, String poolName) {
super(runnable, poolName + "-" + created.incrementAndGet());
setUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.error("Uncaught in thread {}", t.getName(), e);
}
}
);
}
@Override
public void run() {
// 复制 debug 标志以确保一致的值
boolean debug = debugLifecycle;
if (debug) {
logger.info("Created '{}' thread", getName());
}
try {
alive.incrementAndGet();
super.run();
} finally {
alive.decrementAndGet();
if (debug) {
logger.info("Exiting '{}' thread", getName());
}
}
}
public static int getThreadsCreated() {
return created.get();
}
public static int getThreadsAlive() {
return alive.get();
}
public static boolean isDebug() {
return debugLifecycle;
}
public static void setDebug(boolean debug) {
debugLifecycle = debug;
}
}