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

Programming Assignment Unit 3

The document describes a Java program that implements a Clock class, which manages time updating and displaying using two separate threads. The start() method initializes these threads with different priorities, while the stop() method halts the clock's operation. The TimeUpdater thread updates the time every second, and the TimeDisplay thread prints the current time and date to the console in a specified format.

Uploaded by

kamalsami734
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 views3 pages

Programming Assignment Unit 3

The document describes a Java program that implements a Clock class, which manages time updating and displaying using two separate threads. The start() method initializes these threads with different priorities, while the stop() method halts the clock's operation. The TimeUpdater thread updates the time every second, and the TimeDisplay thread prints the current time and date to the console in a specified format.

Uploaded by

kamalsami734
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

Programming Assignment Unit 3

import [Link];
import [Link];

class Clock {
private boolean running = true;

public void start() {


// Create a thread for updating time
Thread timeUpdateThread = new Thread(new TimeUpdater());
// Create a thread for displaying time
Thread timeDisplayThread = new Thread(new TimeDisplay());

// Set priorities
[Link](Thread.MAX_PRIORITY); // Higher
priority for display
[Link](Thread.MIN_PRIORITY); // Lower
priority for updating

// Start threads
[Link]();
[Link]();
}

public void stop() {


running = false;
}

private class TimeUpdater implements Runnable {


@Override
public void run() {
while (running) {
try {
// Sleep for 1 second to update time
[Link](1000);
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
}
}

private class TimeDisplay implements Runnable {


@Override
public void run() {
while (running) {
// Get current time and date
String currentTime = new SimpleDateFormat("HH:mm:ss dd-
MM-yyyy").format(new Date());
// Print the current time
[Link](currentTime);
try {
// Sleep for a short time to avoid flooding the
console
[Link](1000);
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
}
}

public static void main(String[] args) {


Clock clock = new Clock();
[Link]();

// Let the clock run for 10 seconds for demonstration


try {
[Link](10000);
} catch (InterruptedException e) {
[Link]().interrupt();
}

// Stop the clock after 10 seconds


[Link]();
}
}

Output:

14:23:01 22-09-2024
14:23:02 22-09-2024
14:23:03 22-09-2024
14:23:04 22-09-2024
14:23:05 22-09-2024
14:23:06 22-09-2024
14:23:07 22-09-2024
14:23:08 22-09-2024
14:23:09 22-09-2024
14:23:10 22-09-2024
Explanation

Clock Class: The Clock class manages the time updating and displaying functionality.

start() Method: Initializes and starts the threads for time updating and displaying.

stop() Method: Stops the clock by changing the running state to false.

TimeUpdater Class: A thread responsible for updating the time in the background. It sleeps
for 1 second in each iteration.

TimeDisplay Class: A thread that continuously prints the current time and date to the
console in a specified format.

Thread Priorities: The display thread has a higher priority compared to the update thread.

Main Method: Starts the clock and allows it to run for 10 seconds before stopping it.

You might also like