GitHub项目地址:https://github.com/Snowstorm0/learn-sleep
Gitee项目地址:https://gitee.com/Snowstorm0/learn-sleep
Java 在运行 for 循环时,希望可以每个循环暂停一段时间。
使用 sleep 方法即可:
for (int i = 0; i < 10; i++) {
try {
System.out.println("i:" + i + " time:" + getTme());
Thread.sleep(10 * 1000); // 暂停10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
然后定义时间格式:
Long current_time = System.currentTimeMillis();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formateTime = formate.format(current_time);
运行后,可以看到输出:
i:0 time:15:35:06
i:1 time:15:35:16
i:2 time:15:35:26
i:3 time:15:35:36
i:4 time:15:35:46
暂停10秒成功。
学习更多编程知识,请关注我的公众号:

最后修改时间:2023-04-04 11:05:13
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




