0% found this document useful (0 votes)
4 views2 pages

Simple-clock

The document contains a Java program that implements a simple clock application using multithreading. It features a Clock class that formats and displays the current time, a TimeUpdater thread that simulates background logic, and a ClockDisplay thread that updates the displayed time every second. The main method initializes the clock and starts both threads with different priorities.

Uploaded by

belovedvince
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)
4 views2 pages

Simple-clock

The document contains a Java program that implements a simple clock application using multithreading. It features a Clock class that formats and displays the current time, a TimeUpdater thread that simulates background logic, and a ClockDisplay thread that updates the displayed time every second. The main method initializes the clock and starts both threads with different priorities.

Uploaded by

belovedvince
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/ 2

import java.text.

SimpleDateFormat;
import java.util.Date;

class Clock {
private final SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd-MM-
yyyy");

public void displayTime() {


String currentTime = formatter.format(new Date());
System.out.println("Current Time: " + currentTime);
}
}

class TimeUpdater extends Thread {


private final Clock clock;

public TimeUpdater(Clock clock) {


this.clock = clock;
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(1000); // Simulate background logic
} catch (InterruptedException e) {
System.out.println("Updater Interrupted: " + e.getMessage());
}
}
}
}

class ClockDisplay extends Thread {


private final Clock clock;

public ClockDisplay(Clock clock) {


this.clock = clock;
}

@Override
public void run() {
while (true) {
clock.displayTime();
try {
Thread.sleep(1000); // Update every second
} catch (InterruptedException e) {
System.out.println("Display Interrupted: " + e.getMessage());
}
}
}
}

public class SimpleClockApp {


public static void main(String[] args) {
Clock clock = new Clock();

TimeUpdater updater = new TimeUpdater(clock);


ClockDisplay display = new ClockDisplay(clock);

updater.setPriority(Thread.MIN_PRIORITY);
display.setPriority(Thread.MAX_PRIORITY);

updater.start();
display.start();
}
}

You might also like