Thread priorities - setPriority() and getPriority() methods in java


Contents of page :
  • setPriority() method >
  • getPriority() method >
  • Thread Priority range is from 1 to 10.
  • Program to demonstrate setPriority() and getPriority() method >
  • Output analyzation >
  • What is default priority of newly created thread?
  • What is default priority of newly created threadGroup?


setPriority() method >

  • method is used for Changing the priority of thread.
  • Definition of setPriority() method
public final void setPriority(int newPriority)

  • method sets thread priority to newPriority.
  • If newPriority is more than thread’s threadGroup maximum priority, than thread will be set to thread's threadGroup maximum priority.
  • Method is final so it cannot be overridden by sub class.


getPriority() method >

  • method returns the thread’s priority.
  • Definition of getPriority() method
public final int getPriority()

  • Method is final so it cannot be overridden by sub class.


Thread Priority range is from 1 to 10.
Where 1 is minimum priority and 10 is maximum priority.

Thread class provides variables of final static int type for setting thread priority.

   /* The minimum priority that a thread can have. */
   public final static int MIN_PRIORITY = 1;
  /* The default priority that is assigned to a thread. */
   public final static int NORM_PRIORITY = 5;
   /* The maximum priority that a thread can have. */
   public final static int MAX_PRIORITY = 10;

Thread with MAX_PRIORITY is likely to get more CPU as compared to low priority threads. But occasionally low priority thread might get more CPU. Because thread scheduler schedules thread on discretion of implementation and thread behaviour is totally unpredictable.

Thread with MIN_PRIORITY is likely to get less CPU as compared to high priority threads. But occasionally high priority thread might less CPU. Because thread scheduler schedules thread on discretion of implementation and thread behaviour is totally unpredictable.


Program to demonstrate setPriority() and getPriority() method >
class MyRunnable implements Runnable{
   public void run(){
          System.out.println(Thread.currentThread().getName()+"'s priority = "
                    +Thread.currentThread().getPriority());
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ThreadPriority {
   public static void main(String...args){
          MyRunnable myRunnable=new MyRunnable();
          Thread thread1=new Thread(myRunnable,"Thread-1");
          Thread thread2=new Thread(myRunnable,"Thread-2");
          Thread thread3=new Thread(myRunnable,"Thread-3");
          thread1.setPriority(Thread.MAX_PRIORITY); //setting thread priority to 10
          thread2.setPriority(Thread.NORM_PRIORITY);  //setting thread priority to 5
          thread3.setPriority(Thread.MIN_PRIORITY); //setting thread priority to 1
          thread1.start();
          thread2.start();
          thread3.start();
   }
}
/*OUTPUT
Thread-1's priority = 10
Thread-2's priority = 5
Thread-3's priority = 1
*/


Output analyzation >
Thread with MAX_PRIORITY got more CPU as compared to low priority threads. But occasionally low priority thread might get more CPU. Because thread scheduler schedules thread on discretion of implementation and thread behaviour is totally unpredictable.

Thread with MIN_PRIORITY got less CPU as compared to high priority threads. But occasionally high priority thread might less CPU. Because thread scheduler schedules thread on discretion of implementation and thread behaviour is totally unpredictable.



What is default priority of newly created thread?
Thread is initialized with default priority of 5.
But, if threadGroup ’s maximum priority is less than 5 than thread is set to threadGroup’s maximum priority.
Example>
If threadGroup ’s maximum priority = 3
Than unless specified, new thread’s priority will be 3.
Even If specified, new thread’s priority cannot be greater than 3. See example.

class MyRunnable implements Runnable{
   public void run(){
          System.out.println(Thread.currentThread().getName()+"'s priority = "
                       +Thread.currentThread().getPriority());
   }
}
public class ThreadPriority {
   public static void main(String...args){
          MyRunnable myRunnable=new MyRunnable();
          Thread thread1=new Thread(myRunnable,"Thread-1");
          thread1.start();       
   }
}
/*OUTPUT
Thread-1's priority = 5
*/

In the above program, thread is initialized with default priority of 5.
But, if threadGroup ’s maximum priority would have been less than 5 than thread would have been set to threadGroup’s maximum priority.


What is default priority of newly created threadGroup?

Please see ThreadGroup in java.




RELATED LINKS>



Important Thread methods (salient features, usage with programs)>

Sleep() method in threads - 10 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Join() method - 10 salient features of join

Thread isAlive() method

interrupt() method of thread in java

holdsLock(object) method to find whether current thread holds the lock on monitor of specified object.

Thread priorities - setPriority() and getPriority() methods

Using Suspend and resume method in threads



Methods for Adding ShutdownHook and handling uncaught runtime exception >

Threads addShutdownHook method in java

Handling uncaught runtime exception generated in run method using - setDefaultUncaughtExceptionHandler method




No comments:

Post a Comment