e0373f495675001d187dee36bb048aa6
来自「java程序设计教程的源码」· 代码 · 共 34 行
TXT
34 行
/*【例9-7】 设计一个程序,先创建一个线程,每隔1秒钟在命令行窗口输出本地机器的时间;
* 3秒钟后,该线程又被分配了实体。查看运行结果。
*/
//程序清单9-7: Example9_7.java
import java.util.Date;
public class Example9_7 {
public static void main(String args[]) {
TestThread7 test7 = new TestThread7();
Thread thread = new Thread(test7);
thread.start();
}
}
class TestThread7 implements Runnable {
int time = 0;
public void run() {
while (true) {
System.out.println(new Date());
time++;
try {
Thread.sleep(800);
} catch (InterruptedException e) {
}
if (time == 3) {
Thread thread = Thread.currentThread();
thread = new Thread(this);
thread.start();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?