1. 程式人生 > 其它 >Java中執行緒的使用:每隔一秒輸出時間

Java中執行緒的使用:每隔一秒輸出時間

技術標籤:JAVAjava

使用Java提供的執行緒建立方式,建立執行緒類。執行緒的任務是每隔一秒以“09:23:12”的形式向系統輸出裝置列印時間。

import java.time.LocalTime;

public class Test implements Runnable{

	public static void main(String[] args) {
		Test01 test01 = new Test01();
		Thread thread = new Thread(test01);
		thread.start();
	}

	@Override
	public void
run() { while (true) { //while(true)為死迴圈,會一直迴圈輸出時間,可定義int i = 10調整輸出資料數量。 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } LocalTime time = LocalTime.now().withNano(0); System.out.println(time); } } }

在這裡插入圖片描述