MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Thread Priorities


In a Multithreading environment, all the threads which are ready and waiting to be executed are present in the Ready queue. The thread scheduler is responsible for assigning the processor to a thread. But the question is on what basis the thread scheduler decides that a particular thread will run before other threads. Here comes the concept of priority in the picture.

1. Every single thread created in Java has some priority associated with it.
2. The programmer can explicitly assign the priority to the thread. Otherwise, JVM automatically assigns priority while creating the thread.
3. In Java, we can specify the priority of each thread relative to other threads. Those threads having higher priorities get greater access to the available resources than lower priorities threads.
4. Thread scheduler will use priorities while allocating processor.
5. The valid range of thread priorities is 1 to 10 (but not 0 to 10), where 1 is the least priority, and 10 is the higher priority.
6. If there is more than one thread of the same priority in the queue, then the thread scheduler picks any one of them to execute.

The following static final integer constants are defined in the Thread class:


MIN_PRIORITY: Minimum priority that a thread can have. Value is 1.
NORM_PRIORITY: This is the default priority automatically assigned by JVM to a thread if a programmer does not explicitly set the priority of that thread. Value is 5.
MAX_PRIORITY: Maximum priority that a thread can have. Value is 10.

Priority Methods In Java :


setPriority():
This method is used to set the priority of a thread. IllegalArgumentException is thrown if the priority given by the user is out of the range [1,10].
Syntax :
public final void setPriority(int x)   // x is the priority [1,10] that is to be set for the thread.

getPriority():
This method is used to display the priority of a given thread.
Syntax :
t1.getPriority() // Will return the priortity of the t1 thread.

Example


class cwh_Priority extends Thread{  
 public void run(){  
   System.out.println("I'm thread : "+Thread.currentThread().getName());  
   System.out.println("I'm thread :"+Thread.currentThread().getPriority());  
  
  }  
 public static void main(String args[]){  
  cwh_Priority t1=new cwh_Priority();  
  cwh_Priority t2= new cwh_Priority();  
  t1.setPriority(Thread.MIN_PRIORITY);  // setting priority of t1 thread to MIN_PRIORITY (1)
  t2.setPriority(Thread.MAX_PRIORITY);  // setting priority of t2 thread to MAX_PRIORITY (10)
  t1.start();  
  t2.start();  
   
 }  
}   

Output

I'm thread : Thread-0
I'm thread : Thread-1
I'm thread :10
I'm thread :1

Example


package com.company;

class MyThr1 extends Thread{
    public MyThr1(String name){
        super(name);
    }
    public void run(){
        int i = 34;

        while(true){
//            System.out.println("I am a thread");
            System.out.println("Thank you: " + this.getName());
        }

    }
}

public class thread_priorities {
    public static void main(String[] args) {
        // Ready Queue: T1 T2 T3 T4 T5
        MyThr1 t1 = new MyThr1("Harry1");
        MyThr1 t2 = new MyThr1("Harry2");
        MyThr1 t3 = new MyThr1("Harry3");
        MyThr1 t4 = new MyThr1("Harry4");
        MyThr1 t5 = new MyThr1("Harry5 (most Important)");
        t5.setPriority(Thread.MAX_PRIORITY);
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        t3.setPriority(Thread.MIN_PRIORITY);
        t4.setPriority(Thread.MIN_PRIORITY);
        t5.setPriority(Thread.MIN_PRIORITY);
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

    }
}


Conclusion

In this page (written and validated by ) you learned about Java Thread Priorities . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Thread Methods.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.