1. Write a Java program
including two classes:
Class
PrintThread, which inherits from Thread, consists of instance
variable sleepTime, a constructor and a run method. Variable sleepTime stores a random integer
value chosen when a PrintThread object is
constructed. Each PrintThread object sleeps for the amount of time specified by sleepTime then outputs its name.
Class
PrintTest’s main method instantiates four PrintThread objects and invokes the Thread class start method on each one to place
all four PrintThread objects in a ready state. Note that the program
terminates execution when the last PrintThread awakens and prints it name.
Solution:
PrintThread.java, implementation of a Thread
using inheritance.
PrintTest.java, main class used for
testing.
2. Write a Java program to
demonstrate that as a high-priority thread executes, it will delay the
execution of all lower-priority threads.
Solution:
ThreadPreempt.java
TestPreempt.java
3. Write a Java program that
demonstrates a high-priority thread using sleep to give lower-priority threads
a chance to run.
Solution:
ThreadSleep.java
TestSleep.java