我试图找出一些行为。我有一些产生一个线程的代码。它等待一段时间,然后中断它,加入它然后退出该方法。Java线程中断和连接(加入后线程仍然活着)
.
.
.
try {
Thread.sleep(processForMillis);
}
catch (InterruptedException ex) {
// Won't happen, ignore.
}
for (Thread t : threads) {
logger.debug("Interrupting Thread " + t.getName());
t.interrupt();
}
for (Thread t : threads) {
try {
t.join(1000L);
logger.debug("Joined Thread " + t.getName());
logger.debug("isAlive? " + t.isAlive());
}
catch (InterruptedException ex) {
// this will never happen
logger.debug("InterruptionException while joining, but didn't expect it.");
}
}
} // end of method
我目前只用一个线程运行这个。我可以在我的日志中看到,通常情况下,isAlive()在加入后将是错误的,但有时它仍然存在。线程是坐在一个while循环:
while(!Thread.currentThread().isInterrupted()){
.
// do some blocking io stuff here
}
所以我怀疑正在发生的事情,而在读取/处理的InputStream(阻塞IO)是我们中断线程,它正在采取比它花费的时间更多打条件并完成连接。
所以我的问题是,线程会发生什么?
它不再被引用,线程可以被垃圾回收,但没有任何资源被正确清理,而且看起来很糟糕。除了切换到NIO之外,还有更好的模式吗?
是啊,这就是我怀疑。除了NIO或增加(或不使用)加入等待时间,是否有一个很好的模式来解决这个问题? – DanInDC 2010-01-29 16:46:24