JAVA | Explain following thread methods with suitable example.

(1)   setpriority (int max)
(2)   getpriority( ) 

(1)   setpriority (int max):

The setPriority() method of thread class is used to change the thread's priority. Every thread has a priority which is represented by the integer number between 1 to 10.
Thread class provides 3 constant properties: 
1. public static int MIN_PRIORITY: It is the minimum priority of a thread. The value of it is 1. 
2. public static int NORM_PRIORITY: It is the normal priority of a thread. The value of it is 5. 
3. public static int MAX_PRIORITY: It is the maximum priority of a thread. The value of it is 10. 
We can also set the priority of thread between 1 to 10. This priority is known as custom priority or user defined priority.
Syntax:
public final void setPriority(int a)  

(2)   getpriority( ):
The getPriority() method of thread class is used to check the priority of the thread. When we create a thread, it has some priority assigned to it. Priority of thread can either be assigned by the JVM or by the
programmer explicitly while creating the thread.
The thread's priority is in the range of 1 to 10. The default priority of a thread is 5. 
Syntax:
public final int getPriority()   
Example: 
public class PriorityExample extends Thread  
{    
public void run()  
{    
System.out.println("Priority of thread is: "+Thread.currentThread().g
getPriority());    
}    
 public static void main(String args[])  
{    
  PriorityExample t1=new PriorityExample ();    
  t1.setPriority(Thread.MAX_PRIORITY);    
  t1.start();    
    }    
}

Post a Comment

0 Comments