一个计时器

来源:互联网 发布:linux命令别名 编辑:程序博客网 时间:2024/04/28 21:13

编写一个计时器,每隔一秒钟,打出最新时间:
利用线程编写:

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();            }        }    }}
0 0