我正在尝试使用 Thread.join(millis) 使正在运行的线程超时,但它只是挂起并且没有超时。这可能是什么原因造成的?示例代码
public static void main(String[] args) throws InterruptedException {
final Thread t1 = new Thread(() -> {
for (int i = 0; i < 100000000000000000L; i++) {
}
});
t1.start();
t1.join(100);
System.out.println("t1.join called");
}
最佳答案
Thread join(milliseconds) not killing thread in Java
这并不是要杀死线程。它的意思是等待它退出到超时值,然后如果超时到期,则将 InterruptedException
抛给 current 线程。请参阅 Javadoc。
关于java - 线程连接(毫秒)不会在 Java 中杀死线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48615523/