InterviewSolution
Saved Bookmarks
| 1. |
Thread Priority in Java |
|
Answer» In multithreading, each thread is assigned a PRIORITY. The processor is assigned to the thread by the scheduler based on its priority i.e. the highest priority thread is assigned the processor FIRST and so on. The three STATIC values defined in the Thread class for the priority of a thread are given as follows:
This is the maximum thread priority with value 10.
This is the default thread priority with value 5.
This is the minimum thread priority with value 1. A program that demonstrates thread priority in Java is given as follows: import java.lang.*; public class ThreadPriorityDemo extends Thread { public static VOID main(String[]args) { ThreadPriorityDemo thread1 = new ThreadPriorityDemo(); ThreadPriorityDemo thread2 = new ThreadPriorityDemo(); ThreadPriorityDemo thread3 = new ThreadPriorityDemo(); System.out.println("Default thread priority of thread1: " + thread1.getPriority()); System.out.println("Default thread priority of thread2: " + thread2.getPriority()); System.out.println("Default thread priority of thread3: " + thread3.getPriority()); thread1.setPriority(8); thread2.setPriority(3); thread3.setPriority(6); System.out.println("New thread priority of thread1: " + thread1.getPriority()); System.out.println("New thread priority of thread2: " + thread2.getPriority()); System.out.println("New thread priority of thread3: " + thread3.getPriority()); } }The OUTPUT of the above program is as follows: Default thread priority of thread1: 5 Default thread priority of thread2: 5 Default thread priority of thread3: 5 New thread priority of thread1: 8 New thread priority of thread2: 3 New thread priority of thread3: 6 |
|