利用线程实时显示时间
2022-06-08 09:44:50

题目

用线程的方式实现实时显示时钟

思路

理论上来说应该用JFrame配上Date来进行可视化时钟的实现,但是题目并没有过多解释,就用最简单的命令行实时刷新时间显示

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Date;
import java.util.TimeZone;


class Thread1 extends Thread{
public void run()
{
while(true)
{
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
System.out.println(new Date(System.currentTimeMillis()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public class Clock
{
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.start();
}
}