Thread Priority in Java

Category: Java   Tags: Java, Java Thread, Java MultiThread, Thread Synchronization, Thread Priority

Thread priorities are used by the thread scheduler to decide when each thread should be allowed to run. A higher-priority threads get more CPU time than lower-priority threads.

To set the priority of a method use setPriority() method of Thread class. It has below syntax:

                            final void setPriority(int priority)
                          

priority is the priority value. Accepted value of priority for a thread is in range of 1 to 10. Thread class has constants for these value:

  • public static int MIN_PRIORITY: Minimum priority that a thread can have. Value for this is 1.
  • public static int NORM_PRIORITY: This is default priority. Value for this is 5.
  • public static int MAX_PRIORITY: Maximum priority that a thread can have. Value for this is 10.

To get the thread priority, call getPriority() method of Thread class.

JavaThreadPriorityExample.java

                            package com.tutorial.javabasic;

                            class MyThread implements Runnable {
                                String threadName;
                                Thread thread;

                                MyThread(String name) {
                                    threadName = name;
                                    thread = new Thread(this, name);
                                    thread.start();
                                }

                                @Override
                                public void run() {
                                    System.out.println("Inside [ " + threadName + " ] run method.");
                                }
                            }

                            public class JavaThreadPriorityExample {
                                public static void main(String s[]) {
                                    System.out.println("Main thread");
                                    MyThread thread1 = new MyThread("Thread-1");
                                    MyThread thread2 = new MyThread("Thread-2");
                                    MyThread thread3 = new MyThread("Thread-3");

                                    System.out.println("Thread1 priority: " + thread1.thread.getPriority());
                                    System.out.println("Thread2 priority: " + thread2.thread.getPriority());
                                    System.out.println("Thread3 priority: " + thread3.thread.getPriority());

                                    thread1.thread.setPriority(Thread.MIN_PRIORITY);
                                    thread2.thread.setPriority(Thread.NORM_PRIORITY);
                                    thread3.thread.setPriority(Thread.MAX_PRIORITY);

                                    System.out.println("Thread1 priority: " + thread1.thread.getPriority());
                                    System.out.println("Thread2 priority: " + thread2.thread.getPriority());
                                    System.out.println("Thread3 priority: " + thread3.thread.getPriority());
                                }
                            }
                        

Output:

                            Main thread
                            Thread1 priority: 5
                            Thread2 priority: 5
                            Thread3 priority: 5
                            Thread1 priority: 1
                            Thread2 priority: 5
                            Thread3 priority: 10
                            Inside [ Thread-1 ] run method.
                            Inside [ Thread-2 ] run method.
                            Inside [ Thread-3 ] run method.
                          

Default priority for main thread is always 5, you can change it later. Default priority for all other threads depends on the priority of parent thread.

If two threads have same priority then you cannot expect which thread will execute first. It depends on thread scheduler algorithm. Underlying platform should provide support for scheduling based on thread priority.