第16周作业

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

代码

1.ThreadObject

/**
 * 使用Runnable接口创建线程
 */
import java.util.Date;
public  class ThreadObject implements Runnable{
	
	public void run() {    //重写run方法
		Date date;
		while(true){
			date = new Date();		//获取系统时间
			System.out.println(date);		
			try {
				Thread.sleep(1000);   	//线程每隔一秒进行休眠
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
		
	}

}

2.Test

public class Test {
	public static void main(String[] args) {
		Thread time = new Thread (new ThreadObject());  //创建线程time
		time.start();
			
	}

}

运行结果

 

posted @ 2019-12-20 12:30  朱佳美20194662  阅读(122)  评论(0编辑  收藏  举报