Java学习-072-多线程05:线程休眠 Thread.sleep()
可通过 Thread.sleep(millis) 实现线程的休眠,用于实际业务需求开发需要的等待时间。示例源代码如下所示:
package com.fanfengping.demo;import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Demo09RunnableSleep implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
if (i > 1) {
try {
log.info("{} 开始休眠", Thread.currentThread().getName());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info("{} 运行,i = {}", Thread.currentThread().getName(), i);
}
}
public static void main(String[] args) {
Demo09RunnableSleep demo09RunnableSleep = new Demo09RunnableSleep();
Thread threadJoin = new Thread(demo09RunnableSleep, "线程休眠");
threadJoin.start();
}
}
程序输出如下所示:
以上是 Java学习-072-多线程05:线程休眠 Thread.sleep() 的全部内容, 来源链接: utcz.com/z/393454.html