下面是代码:
@Test
public void test03() throws Exception {
// 启动线程
new Thread(MyServer::run, "Th01").start();
}
class MyServer {
private static int total = 10;
public static synchronized void run() {
try {
for (int i = 1; i < total; i++) {
System.out.println(Thread.currentThread().getName() + " :" + i);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
本来预期应该能把1到10都打出来,但是debug时发现运行到sleep时直接线程终止了。。。
如果去掉sleep方法就能正常打印完,大佬们这是怎么回事啊?求解,谢谢!
