java-创建一个线程,在控制台不断输出当前时间,精确到时分秒,每隔一秒输出一次。

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

/**
 * 创建一个线程,在控制台不断输出当前时间,精确到时分秒,每隔一秒输出一次。
 * 
 * @author 死胖纸
 *
 * @time 2018-7-18
 *
 */
public class test {

	public static void main(String[] args) {

		Thread thread = new MyThread();
		thread.start();
		
	}

}


// 通过继承Thread类 来重写run方法
class MyThread extends Thread {

    //通过正则式来设置输出的时间格式
	SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");

    //重写run()方法
	public void run() {
		while (true) {
			String str = s.format(new Date());
			System.out.println(str);
			try {
                //间隔时间1秒
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/m0_37489491/article/details/81092474
今日推荐