准备有个是中线程
package com.foot.lesson4;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TimerThread extends Thread {
private DateFormat df = null;
private Calendar calender = null;
public TimerThread() {
this.df = new SimpleDateFormat("HH:mm:ss");
this.calender = Calendar.getInstance();
}
@Override
public void run() {
while(true) {
System.out.println(df.format(calender.getTime()));
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
this.calender.add(Calendar.SECOND, 1);
}
}
}
运行该时钟线程
package com.foot.lesson4;
public class App {
public static void main(String[] args) {
TimerThread tt = new TimerThread();
tt.start();
}
}
运行结果
16:28:28
16:28:29
16:28:30
16:28:31
16:28:32