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

//模拟倒计时 : 放大问题的反生性
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;
            }
        }

    }


}

猜你喜欢

转载自blog.csdn.net/qq_42794826/article/details/108957030
今日推荐