0 votes
182 views
in Java by
What is Java thread priority? Please explain thread priority with running example.

1 Answer

0 votes
by (4.4k points)

First of all, i am going to example about java thread priority.

Priority of Thread:- Each thread have a priority. Priorities are represented by a number between 1 and 10.

There are 3 constants in java Thread class:-

  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int MAX_PRIORITY

To set thread priority by using setPriority() method. It takes int value.

See the following running example of Thread priority:-

package threadExample;

public class TestThreadPriority extends Thread{

    public void run(){
        System.out.println("Thread name: "
                + Thread.currentThread().getName()
                + " priority: "
                + Thread.currentThread().getPriority());  
      
    }

    public static void main(String[] args) {
        TestThreadPriority threadPriority1 = new TestThreadPriority();
        TestThreadPriority threadPriority2 = new TestThreadPriority();
        threadPriority1.setPriority(Thread.MIN_PRIORITY);
        threadPriority2.setPriority(Thread.MAX_PRIORITY);
        threadPriority1.start();  
        threadPriority2.start();  
    }

}

Output:-

Thread name: Thread-0 priority: 1
Thread name: Thread-1 priority: 10

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...