By
giving priorities to threads, we tell the JVM, give more importance
to Highest priority threads, and low importance to low priority
threads.
1.
How to get the priority of a Thread
java
provides getPriority(), to get the priority of a running thread.
Syntax
public
final int getPriority()
Returns
this thread's priority. This method is final, so we can't override
this method
2.
By Default main thread runs at the priority of 5.
Example class ThreadPriorityEx{ public static void main(String args[]){ System.out.println("Main Thread Priority is " + Thread.currentThread().getPriority()); } }
Output
Main Thread Priority is 5
3.
How to set the priority to a Thread
java
provides setPriority(), to set the priority of a running thread.
Syntax
public
final void setPriority(int newPriority)
Changes
the priority of this thread.
4.
when a new Thread is constructed, it runs at the same priority as the
thread that constructed it.
Example
class ThreadPriorityEx implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName() + "Priority is" + Thread.currentThread().getPriority()); } public static void main(String args[]){ ThreadPriorityEx task = new ThreadPriorityEx(); Thread.currentThread().setPriority(8); System.out.println("Main Thread Priority is " + Thread.currentThread().getPriority()); Thread t1 = new Thread(task); t1.setName("MyThread"); t1.start(); } }
Output
Main Thread Priority is 8 MyThreadPriority is 8
5.
Thread Priority Constants
Thread
API provides three constants that specify thread priorities
static
int MAX_PRIORITY
The
maximum priority that a thread can have. Generally this value is 10.
static
int MIN_PRIORITY
The
minimum priority that a thread can have. Generally this value is 1.
static
int NORM_PRIORITY
The
default priority that is assigned to a thread. Generally this value
is 5.
Example
class ThreadPriorityEx{ public static void main(String args[]){ System.out.println("Maximum priority of a thread is " + Thread.MAX_PRIORITY); System.out.println("Minimum priority of a thread is " + Thread.MIN_PRIORITY); System.out.println("Normal priority of a thread is " + Thread.NORM_PRIORITY); } }
Output
Maximum priority of a thread is 10 Minimum priority of a thread is 1 Normal priority of a thread is 5
6.
If you try to set a priority which is less than MIN_PRIORITY and
greater than MAX_PRIORITY, then program compiles fine, but throws
“IllegalArgumentException” at run time.
class ThreadPriorityEx implements Runnable{ public void run(){ } public static void main(String args[]){ ThreadPriorityEx task1 = new ThreadPriorityEx(); int priority1 = Thread.MAX_PRIORITY + 1; int priority2 = Thread.MIN_PRIORITY - 1; Thread t1 = new Thread(task1); Thread t2 = new Thread(task1); t1.setPriority(priority1); t2.setPriority(priority2); } }
Output
Exception in thread "main" java.lang.IllegalArgumentException at java.lang.Thread.setPriority(Unknown Source) at ThreadPriorityEx.main(ThreadPriorityEx.java:16)
Thread
group priorities reflect the thread priorities. Will discuss about
more on these in the Thread Group post.
No comments:
Post a Comment