com.sun.messaging.jmq.jmsserver.service.HAMonitorService 类包含 HATimerThread,它启动一个 java 线程,然后运行它( HATimerThread 是一个 Runnable )。
该线程不是守护线程。
运行代码如下;
public void run() {
while (true) {
long time = System.currentTimeMillis();
synchronized(this) {
if (time < nexttime) {
try {
this.wait(nexttime - time);
} catch (InterruptedException ex) {}
continue;
} else {
child.run();
}
}
if (repeatItr == 0) break;
time = System.currentTimeMillis();
long tmpnext = nexttime + repeatItr;
if (time >= tmpnext) {
nexttime = time;
} else {
nexttime = tmpnext;
}
}
}
该线程可以退出的唯一方法是如果repeatItr == 0。但是,一旦在构造函数中设置(由HAMonitorService 的构造函数调用,它调用它使其不为0)似乎没有任何改变。这意味着循环永远不会退出,这意味着线程永远不会停止,并且由于它不是守护线程,因此 VM 永远不会关闭。
这是一个错误还是有其他一些我没有想到的阻止它的机制?目前,运行我的嵌入式集群代理的进程永远不会因此而退出,即使它的其余部分干净地关闭......
试过这个使用 4.5.2 和 5.1。
我重建了 5.1 的源代码,将 HATimerThread 创建的线程设置为守护进程,现在一切正常。