本文共 964 字,大约阅读时间需要 3 分钟。
package duoxiancheng;//模拟网络延时public class TestSleep { public static void main(String[] args) throws InterruptedException { turnDown(); } public static void turnDown() throws InterruptedException { int num=10; while(true){ Thread.sleep(1000); System.out.println(num--); if(num<=0){ break; } } }}利用sleep打印输出当前系统时间:
package duoxiancheng;import java.text.SimpleDateFormat;import java.util.Date;//模拟网络延时public class TestSleep { public static void main(String[] args) throws InterruptedException { Date startTime=new Date(System.currentTimeMillis());//获取系统当前时间 while(true){ Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime)); Date smartTime=new Date(System.currentTimeMillis());//更新系统当前时间 } }}
代码与截图如下:
转载地址:http://wznwi.baihongyu.com/