Java多线程之sleep方法阻塞线程-模拟时钟
1 package org.study2.javabase.ThreadsDemo.status; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 /** 7 * @Auther:GongXingRui 8 * @Date:2018/9/19 9 * @Description: 进程休眠(sleep)-模拟时钟 10 **/ 11 public class ThreadSleep { 12 public static void main(String args[]) throws Exception { 13 ThreadSleep demo = new ThreadSleep(); 14 demo.time(); 15 } 16 17 public void time() { 18 while (true) { 19 Date curTime = new Date(System.currentTimeMillis()); 20 System.out.println("原始格式:" + curTime); 21 long timeNum = curTime.getTime(); 22 System.out.println("总毫秒: " + timeNum); 23 String time = new SimpleDateFormat("hh:mm:ss").format(curTime); 24 System.out.println(time); 25 try { 26 Thread.sleep(1000); 27 } catch (InterruptedException e) { 28 e.printStackTrace(); 29 } 30 } 31 } 32 }