Java第16周作业集

一、题目

编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。

二、源代码

package com;
import java.util.Date;
public class ThreadTime extends Thread {
    public void run(){//重写run方法
        Date date;
        while(true){
            date=new Date();
            System.out.println(date);//输出当前时间
            try {
                this.sleep(2000);//休眠2秒
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ThreadTime threadTime=new ThreadTime();
        threadTime.start();//启动Thread线程
    }
}

三、运行结果

原文地址:https://www.cnblogs.com/jingxueyan/p/12075082.html