我想测试 Thread.sleep() 方法,我发现了一个有趣的事情。当我调用 main() 方法时,控制台会打印“UserA sleep...”和“UserA waking...”,这意味着程序被唤醒,但是当我使用 junit 方法运行与 main() 方法相同的代码时,它不会打印“UserA waking...”......我将不胜感激任何人都可以解释它。
package com.lmh.threadlocal;
import org.junit.Test;
public class ThreadTest {
public static void main(String [] args) {
new Thread(new UserA()).start();
}
@Test
public void testWakeup(){
new Thread(new UserA()).start();
}
}
class UserA implements Runnable{
@Override
public void run() {
try {
System.out.println("UserA sleeping...");
Thread.sleep(1000);
System.out.println("UserA waking...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}