-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
42 lines (37 loc) · 1.47 KB
/
Clock.java
File metadata and controls
42 lines (37 loc) · 1.47 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.text.SimpleDateFormat;
import java.util.Date;
public class Clock {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
// Method to continuously update and print the current time
public static void updateAndPrintTime() {
Thread updateTimeThread = new Thread(() -> {
while (true) {
Date now = new Date();
System.out.println("Current Time: " + dateFormat.format(now));
try {
Thread.sleep(1000); // Update time every second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
updateTimeThread.setPriority(Thread.MIN_PRIORITY); // Lower priority for background updating thread
Thread printTimeThread = new Thread(() -> {
while (true) {
Date now = new Date();
System.out.println("Current Time: " + dateFormat.format(now));
try {
Thread.sleep(1000); // Update time every second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
printTimeThread.setPriority(Thread.MAX_PRIORITY); // Higher priority for clock display thread
updateTimeThread.start();
printTimeThread.start();
}
public static void main(String[] args) {
updateAndPrintTime();
}
}