休眠线程有两种实现: 使⽤ sleep 休眠和使⽤ TimeUnit 休眠。
1.使⽤ sleep 休眠
方法 | 说明 |
public static void sleep(long millis) throws InterruptedException | 休眠当前线程 millis 毫秒 |
public static void sleep(long millis) throws InterruptedException | 休眠当前线程 millis 毫秒 |
public class CsdnDemo {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
try {
Thread.sleep(60 * 60 * 1000);
} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("我接到了一个终止执行的通知");
}
});
thread.start();
Thread.sleep(1000