- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
Latest commit
42 lines (37 loc) · 1.47 KB
/
Copy pathClock.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
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassClock {
privatestaticfinalSimpleDateFormatdateFormat = newSimpleDateFormat("HH:mm:ss dd-MM-yyyy");
// Method to continuously update and print the current time
publicstaticvoidupdateAndPrintTime() {
ThreadupdateTimeThread = newThread(() -> {
while (true) {
Datenow = newDate();
System.out.println("Current Time: " + dateFormat.format(now));
try {
Thread.sleep(1000); // Update time every second
} catch (InterruptedExceptione) {
e.printStackTrace();
}
}
});
updateTimeThread.setPriority(Thread.MIN_PRIORITY); // Lower priority for background updating thread
ThreadprintTimeThread = newThread(() -> {
while (true) {
Datenow = newDate();
System.out.println("Current Time: " + dateFormat.format(now));
try {
Thread.sleep(1000); // Update time every second
} catch (InterruptedExceptione) {
e.printStackTrace();
}
}
});
printTimeThread.setPriority(Thread.MAX_PRIORITY); // Higher priority for clock display thread
updateTimeThread.start();
printTimeThread.start();
}
publicstaticvoidmain(String[] args) {
updateAndPrintTime();
}
}