编写一个计时器,每隔一秒钟,打出最新时间:
利用线程编写:
import java.util.Date;
public class TimerTest {
public static void main(String[] args) throws InterruptedException
{
PrintTime pt = new PrintTime();
pt.run();
}
}
class PrintTime implements Runnable
{
public void run()
{
while(true)
{
System.out.println("现在时间是:" + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}