javaSE之多线程使用sleep模拟倒计时

    科技2025-01-10  11

    //模拟倒计时 : 放大问题的反生性 public class TestSleep { public static void main(String[] args) { //打印系统当前时间 Date startDate =new Date(System.currentTimeMillis()); while (true){ try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startDate)); //更新时间 startDate =new Date(System.currentTimeMillis());//跟新当前时间 } catch (InterruptedException e) { e.printStackTrace(); } } // try { // tenDown(); // } catch (InterruptedException e) { // e.printStackTrace(); // } } public static void tenDown() throws InterruptedException { int num=10; while (true){ Thread.sleep(1000); System.out.println(num--); if (num<=0){ break; } } } }
    Processed: 0.012, SQL: 8