Java零基础学java之多线程--09线程休眠

线程休眠

package com.li.thread_state;


import java.text.SimpleDateFormat;
import java.util.Date;

//模拟倒计时
public class TestThreadSleep {
    
    
    public static void main(String[] args) {
    
    
        //模拟系统时间

        while (true) {
    
    
            try {
    
    
                Thread.sleep(1000);
                Date startDate = new Date();//获得当前系统时间
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                System.out.println(simpleDateFormat.format(startDate));//格式化时间,字符串-->日期
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }



    }
//        try {
    
    
//            tenDown();
//        } catch (InterruptedException e) {
    
    
//            e.printStackTrace();
//        }
//    }
    //模拟10秒倒计时
    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/l1341886243/article/details/118355285