TestNG优雅地终止了线程.知道为什么吗



我使用的是TestNG版本7.1.0,这是我的示例代码:

@Test
public void testThreads(){
new Thread(() -> {
System.out.println("This is printed");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("This is not printed");
}).start();
}

此处的输出仅为This is printed,而忽略第二行。没有引发异常。然而,如果我这样做:

public static void main(String[] args) {
new Thread(() -> {
System.out.println("This is printed");
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("This is not printed");
}).start();
}

两行都按预期打印。你能解释一下为什么观察到这种奇怪的行为吗?

TestNG使用对System.exit的调用来执行测试用例。这将终止您启动的线程。如果在测试方法结束时添加明显超过100毫秒的等待时间,那么在启动显式线程后,您将看到第二个print语句。

最新更新