2. Objectives
On completion of this period, you would be
able to learn
• Concepts of thread priority
• Relevant methods and constants
• Example program
2
3. Recap
• Before discussing thread priority, let’s quickly
recollect some concepts on thread
• Java provides two ways for creating threads
• By extending Thread class
• By implementing Runnable interface
• In each of these ways
• run() method has to be overridden /
implemented
• run() method is the body of the thread
• We use start() method to start the thread
• start() method in-turn calls run() method
3
4. Concepts of Thread Priority
• In operating systems, we learnt the CPU
scheduling algorithms
• Priority scheduling is one of them
• Higher priority processes will be scheduled first
• The same concept can be applied to threads also
4
5. Concepts of Thread Priority contd..
• Why priorities?
• Determine which thread receives CPU control and gets to be
executed first
• Definition:
• Integer value ranging from 1 to 10
• Higher the thread priority larger chance of being executed first
• Example:
• Two threads are ready to run
• First thread: priority of 5, already running
• Second thread : priority of 10, comes in while first thread is
running
• First thread will be preempted from CPU and the CPU will
allotted to the second thread
5
6. Concepts of Thread Priority contd..
• Context switch
• Occurs when a thread snatches the control of CPU from
another
• When does it occur?
• Running thread voluntarily relinquishes CPU control
• Running thread is preempted by a higher priority
thread
• More than one highest priority thread that is ready to run
• Deciding which receives CPU control depends on the
operating system
• Windows 95/98/NT: Uses time-sliced round-robin
• Solaris: Executing thread should voluntarily
relinquish CPU control
6
8. Thread Constants contd..
• When we create a thread directly (not inheriting
any other thread)
• The priority of that thread is
NORM_PRIORITY
• Inherited thread gets the priority of its parent
thread
8
9. Thread Priority methods
• Priority related methods
• setPriority( int priorityNumber )
• sets the priority of the thread
• getPriority( )
• returns the priority value of a thread
9
10. Example Program
class clicker implements Runnable {
int click = 0;
Thread t;
private volatile boolean running = true;
public clicker(int p) {
t = new Thread(this);
t.setPriority(p);
}
public void run() { Setting the priority
while (running) {
click++;
}
} 10
11. Example Program
public void stop() {
running = false;
}
public void start() {
t.start();
}
}
11
12. Example Program Contd..
class HiLoPri {
public static void main (String args[]) {
Thread. current Thread(). setPriority (Thread. MAX_PRIORITY);
clicker hi = new clicker (Thread. NORM_PRIORITY + 2);
clicker lo = new clicker (Thread. NORM_PRIORITY - 2);
lo. start();
Using Priority
hi. start(); constants
try {
Thread.sleep(5000);
} catch (Interrupted Exception e) {
System. out. println ("Main thread interrupted.");
}
12
13. Example Program
lo.stop();
hi.stop();
// Wait for child threads to terminate.
try {
hi.t.join();
lo.t.join();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Low-priority thread: " + lo.click);
System.out.println("High-priority thread: " + hi.click);
}
}
13
14. Example Program Contd..
Output
For the given 5 seconds
The higher priority thread get more number of clicks
compared to the lower priority thread
14
15. Summary
• In this class you have learnt
• The thread priority concepts
• Thread’s priority related constants
• Thread’s priority related methods
• An example program to demonstrate thread
priority
15
16. Frequently Asked Questions
1. Explain the concept of thread priority
2. Which methods are useful for working with thread
priority ?
3. List the thread priority constants
16
17. Quiz
1. What is the range of thread priorities in Java?
1. 0 to 10
2. 1 to 10
3. 1 to 20
4. Any number
17
18. Quiz Contd..
2.What is the default priority set for a newly
created thread ?
1. MAX_PRIORITY
2. MIN_PRIORITY
3. NORM_PRIORITY
4. No priority is set
19. Quiz Contd..
3.Which is NOT a thread priority related method ?
1. setPriority ()
2. setPriorities()
3. yield()
4. getPriority()
19
20. Quiz
4.If the thread priority is higher
1. Less chance for it to get executed first
2. Greater chance for it to get executed first
3. Equal chance for it to get executed first
4. No chance for it to get executed first
21. Assignments
• Write Java programs to create two threads, one
printing odd numbers and the other printing even
numbers
• Set the priority as MAX_PRIORITY - 2 for the first
thread and MIN_PRIORITY + 3 for the second
thread
• Write an application in Java to test the two threads
21