packageexception.Demo03;//模拟倒计时publicclassTestSleep2{publicstaticvoidmain(String[] args){try{tenDown();}catch(Exception e){thrownewRuntimeException(e);}}publicstaticvoidtenDown()throwsInterruptedException{int num =10;while(true){Thread.sleep(1000);System.out.println(num--);if(num<=0){break;}}}}
模拟系统时间:
packageexception.Demo03;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassTestSleep3{//模拟系统时间publicstaticvoidmain(String[] args){//打印当前系统时间Date startTime =newDate(System.currentTimeMillis());//获取系统当前时间while(true){try{Thread.sleep(1000);System.out.println(newSimpleDateFormat("HH:mm:ss").format(startTime));
startTime =newDate(System.currentTimeMillis());//更新当前时间}catch(InterruptedException e){thrownewRuntimeException(e);}}}publicstaticvoidtenDown()throwsInterruptedException{int num =10;while(true){Thread.sleep(1000);System.out.println(num--);if(num<=0){break;}}}}