本文目录导读:

- 基础方式:Thread.sleep()
- 配合TimeUnit设置更直观的时间单位
- 在Runnable线程中使用休眠
- 自定义间隔执行(定时任务模拟)
- 精确休眠(纳秒级别)
- 休眠 + 线程中断处理(最佳实践)
- 重要注意事项
- 完整示例:多线程休眠应用
在Java中设置线程休眠有多种方式,以下是几种常见的实现方法和最佳实践:
基础方式:Thread.sleep()
public class ThreadSleepDemo {
public static void main(String[] args) {
System.out.println("开始执行");
try {
// 休眠3秒
Thread.sleep(3000); // 参数为毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("3秒后执行完毕");
}
}
配合TimeUnit设置更直观的时间单位
import java.util.concurrent.TimeUnit;
public class TimeUnitSleepDemo {
public static void main(String[] args) {
System.out.println("开始执行");
try {
// 使用TimeUnit使代码更清晰
TimeUnit.SECONDS.sleep(3); // 休眠3秒
// TimeUnit.MINUTES.sleep(1); // 休眠1分钟
// TimeUnit.HOURS.sleep(2); // 休眠2小时
// TimeUnit.MILLISECONDS.sleep(500); // 休眠500毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行完毕");
}
}
在Runnable线程中使用休眠
public class RunnableSleepDemo {
public static void main(String[] args) {
Runnable task = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("任务执行第" + (i + 1) + "次");
try {
Thread.sleep(1000); // 每次休眠1秒
} catch (InterruptedException e) {
System.out.println("线程被中断");
return;
}
}
};
Thread thread = new Thread(task);
thread.start();
}
}
自定义间隔执行(定时任务模拟)
public class IntervalTaskDemo {
public static void main(String[] args) {
Thread timerThread = new Thread(() -> {
int count = 0;
while (count < 10) {
System.out.println("第" + (count + 1) + "次执行时间:" +
System.currentTimeMillis());
try {
Thread.sleep(2000); // 每2秒执行一次
} catch (InterruptedException e) {
System.out.println("定时任务被中断");
break;
}
count++;
}
});
timerThread.start();
}
}
精确休眠(纳秒级别)
public class PreciseSleepDemo {
public static void main(String[] args) {
try {
// 第一个参数:毫秒,第二个参数:纳秒
Thread.sleep(1000, 500000); // 休眠约1.5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
休眠 + 线程中断处理(最佳实践)
public class BestPracticeDemo {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
System.out.println("工作线程开始执行");
try {
// 休眠5秒
Thread.sleep(5000);
System.out.println("休眠完成,继续执行");
} catch (InterruptedException e) {
// 恢复中断状态
Thread.currentThread().interrupt();
System.out.println("线程被中断,清理资源并退出");
// 执行必要的清理操作
return;
}
});
worker.start();
// 主线程等待2秒后中断工作线程
Thread.sleep(2000);
worker.interrupt(); // 中断工作线程的休眠
}
}
重要注意事项
必须处理InterruptedException
Thread.sleep()会抛出InterruptedException- 推荐捕获后恢复中断状态:
Thread.currentThread().interrupt()
休眠精度问题
Thread.sleep()的精度依赖于操作系统和JVM- 不保证精确的休眠时间,特别是毫秒以下级别
避免在生产代码中使用Thread.sleep()
- 对于定时任务使用
ScheduledExecutorService - 对于等待条件使用
wait()/notify()或Lock/Condition
推荐使用TimeUnit
// 比下面更清晰 TimeUnit.SECONDS.sleep(3); // 比下面更清晰 Thread.sleep(3000);
完整示例:多线程休眠应用
import java.util.concurrent.TimeUnit;
public class MultiThreadSleepDemo {
public static void main(String[] args) {
// 创建3个线程,不同休眠时间
Thread t1 = new Thread(() -> sleepTask("线程1", 1));
Thread t2 = new Thread(() -> sleepTask("线程2", 2));
Thread t3 = new Thread(() -> sleepTask("线程3", 3));
t1.start();
t2.start();
t3.start();
}
private static void sleepTask(String name, int seconds) {
try {
System.out.println(name + "开始休眠" + seconds + "秒");
TimeUnit.SECONDS.sleep(seconds);
System.out.println(name + "休眠结束");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println(name + "被中断");
}
}
}
选择哪种方式取决于你的具体需求:
- 简单场景:
Thread.sleep() - 需要可读性:
TimeUnit.SECONDS.sleep() - 定时任务:
ScheduledExecutorService - 精确控制:考虑使用
LockSupport.parkNanos()
版权声明:足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
足球直播
足球直播
足球直播
足球直播
足球直播
中超直播
中超直播
中超直播
中超直播
中超直播
英超直播
英超直播
英超直播
英超直播
英超直播
德甲直播
德甲直播
德甲直播
德甲直播
德甲直播
西甲直播
西甲直播
西甲直播
西甲直播
西甲直播
意甲直播
意甲直播
意甲直播
意甲直播
意甲直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播