0% found this document useful (0 votes)
3 views4 pages

Programming2 Unit 3

The document presents a Java program that implements a Clock class to display the current time using two threads: one for displaying the time and another for background updates. It includes error handling for InterruptedExceptions to ensure smooth operation. The code is modular, separating time update logic from display logic.

Uploaded by

robert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Programming2 Unit 3

The document presents a Java program that implements a Clock class to display the current time using two threads: one for displaying the time and another for background updates. It includes error handling for InterruptedExceptions to ensure smooth operation. The code is modular, separating time update logic from display logic.

Uploaded by

robert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ROBERT BANDA

PROGRAMMING 2

UNIT3 ASSIGNMENT

The Clock class

Clock.java
import java.text.SimpleDateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class clock {
// Method to updating and printing the current time
public void displayTime(){
while (true){
Date currentTime= new Date();
SimpleDateFormat formatter = new sssss
String formattedTime = formatter.format(currentTime);
System.out.println("Current Time: " + formattedTime);
try {
Thread.sleep(2000); // sleep for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

The clock Threads

Clock app.java
public class ClockApp {
public static void main(String[] args) {
//example of a clock class
Clock clock = new Clock();
// create and start the clock display thread with higher priority
s displayTread.setPriority(Thread.MAX_PRIORITY);
displayTread.start();
// creating and start background updating thread with lower priority
Thread updateTread = new Thread(() -> {
while (true) {
// simulate background tasks or updates
try {
Thread.sleep(6000);// sleep for 6 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
updateTread.setPriority(Thread.MIN_PRIORITY);
updateTread.start();
}
}
Explanation

Imports.

Java.text.SimpleDateFormart: used to format the date and time string

Java.util.Date: Represent the current date and time.

TimeUpdateer Class:

An extension of Thread; This is responsible for continuously getting the


time and updating an internal variable with the formatted string.

Private currentTime: stores format current time string

Clockclass:This is the main class where program executes

In summary, this code effectively separate the time update logic from the
printing logic using two threads,making it more modular.
Error handling is implemented to catch InterruptedExceptions during the
thread sleeps,ensuring the the program behaves smooth fully even if
interrupted.

Reference:

Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX


edition. Licensed Under CC 4.0. https://math.hws.edu/javanotes/

You might also like