Programming Assignment Unit 3
Programming Assignment Unit 3
java
Code:
import java.text.SimpleDateFormat;
import java.util.Date;
timeUpdaterThread.start();
timePrinterThread.start();
}
Explanation
1. Clock Class:
I. The Clock class contains the main logic for the application.
II. startClock method initializes and starts the threads for updating and printing the
time.
2. TimeUpdater Thread:
I. A background thread that simulates time updates (in this simple example, it just
sleeps for 1 second continuously).
II. This thread has a lower priority.
3. TimePrinter Thread:
I. A thread that prints the current time to the console every second.
II. This thread has a higher priority to ensure more precise time display.
4. Thread Priorities:
I. timeUpdaterThread.setPriority(Thread.MIN_PRIORITY); sets the updater thread
to the lowest priority.
II. timePrinterThread.setPriority(Thread.MAX_PRIORITY); sets the printer thread
to the highest priority.
Screenshot: