第16周作业

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

package factorial;

import java.util.Date;

public class DateTime implements Runnable {

    
    public void run() {
        Date time;
        while(true) {
            time = new Date();
            System.out.println(time);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

}
package factorial;

public class Time1 {

    public static void main(String[] args) {
        Thread test = new Thread(new DateTime());//目标对象
        test.start();

    }

}

运行结果:

 

posted @ 2019-12-22 11:52  tonglingren  阅读(110)  评论(0编辑  收藏  举报