package com.chengguo.线程; import java.text.SimpleDateFormat; import java.util.Date; /** * 模拟倒计时 */ public class Demo_20200518001_Sleep { public static void main(String[] args) { try { tenDown(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("================================"); /** * 打印系统当前时间 */ //获取系统当前时间 Date startTime = new Date(System.currentTimeMillis()); // while (true) { try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime)); //更新系统时间 startTime = new Date(System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void tenDown() throws InterruptedException { int num = 10; while (true) { Thread.sleep(1000); System.out.println("倒计时:" + num-- + "s"); if (num <= 0) { break; } } } }