第16周作业

一、题目1

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

二、代码

Test1.java

/*
 * 创建一个Time类继承Thread类,重写run方法,在主方法中创建time对象,并调用Thread类中的start方法运行
 */
package d;

import java.util.Date;

class Time extends Thread{    
    public void run() {//重写run方法
        while(true){
            System.out.println(new Date());//输出当前时间
            try {
                Thread.sleep(1000);//休眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }
    
}
public class Test1 {

    public static void main(String[] args) {
        Time time = new Time();
         time.start();//启动线程

    }

}

三、运行结果

原文地址:https://www.cnblogs.com/weiyiren666/p/12056740.html