【发布时间】:2015-08-31 22:41:28
【问题描述】:
我看到@Tej Kiran 提出的一个问题here,这正是我的问题,但没有得到回答,最后一条评论说:
"你知道你的系统中是否注册了任何关闭钩子吗? 应用程序,或者如果您正在使用的任何库具有 关机钩子?关闭钩子是一个线程,如果有 该线程中的死锁导致它永远不会终止,JVM 将 永远不要退出。”
我的程序中有一个方法关闭钩子
Runtime.getRuntime().addShutdownHook(new Thread("Shutdown Hook") {
@Override
public void run() {
System.out.println("---------------");
System.out.printf("%d threads running%n", Thread.activeCount());
Map<Thread, StackTraceElement[]> threads = Thread
.getAllStackTraces();
for (Entry<Thread, StackTraceElement[]> e : threads.entrySet()) {
Thread t = e.getKey();
System.out.printf("%s\t%s\t%s%n", t.getName(),
t.isDaemon(), t.isAlive());
StackTraceElement[] elements = e.getValue();
for (StackTraceElement trc : elements) {
System.out.println("\t" + trc);
}
}
System.out.println("---------------");
try {UIUtil.cancelAllTasks();} catch (Throwable e) {e.printStackTrace();};
try {mehad.io.port.ScannerManager.disableAutoHandshake();} catch (Throwable e) {e.printStackTrace();};
try {mehad.io.port.ComPortInterface.getInstance().close();} catch (Throwable e) {e.printStackTrace();};
try {
if (lockStream != null) {
lockStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
但是我不知道如何区分我的shutdown hook中是否有死锁,如果有,如何解决。
【问题讨论】:
-
你试过 kill -3 吗?还是在 wows jstack 上?
-
我不知道该怎么做?! @ThomasKrieger
-
你用的是windows还是linux?
-
我正在使用窗口@ThomasKrieger
-
@Tej Kiran ,你的问题解决了吗?!怎么样?
标签: java multithreading runtime system.exit