@Test
public void testSychronized() throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
/*System.out.println("I Hate U");
synchronized (this){
System.out.println("I Love U");
}*/
while (true){
System.out.println("子线程");
}
}
});
t1.setDaemon(false);
t1.start();
Thread.sleep(2000);
System.out.println("sss");
}
使用junit执行这段代码,会在test方法执行完后,子线程也被强制终止,而使用main测试则不会(当然如果设置了子线程为守护线程也会在主线程执行完,即执行System.exit(0)后被强制退出),这时jvm进程已被终止。
解答引自http://bbs.csdn.net/topics/391807147的网友解答,万分感谢