第16周作业
题目1:编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示。
代码
1.ThreadObject.java
/**
* 创建线程类,定义date对象
*/
import java.util.Date;
public class ThreadObject implements Runnable{
public void 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.java
public class Test {
public static void main(String[] args) {
Thread time = new Thread(new ThreadObject()); //声明线程对象
time.start(); //时time线程进入等待队列
}
}
运行结果

浙公网安备 33010602011771号