Java - Thread Priority

[Updated: Feb 7, 2017, Created: Mar 21, 2016]

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.

Let's test the above statement with an example.


public class ThreadPriorityExample {
    public static void main (String[] args) {
        MyThread thread = new MyThread();
        thread.setName("thread 1");
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();

        Thread thread2 = new MyThread();
        thread2.setName("thread 2");
        thread2.setPriority(Thread.MAX_PRIORITY);
        thread2.start();
    }

    private static class MyThread extends Thread {
        private int c;

        @Override
        public void run () {
            String threadName = Thread.currentThread()
                                      .getName();

            System.out.println(threadName + " started.");
            for (int i = 0; i < 1000; i++) {
                c++;
                try {
                    TimeUnit.MICROSECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(threadName + " ended.");
        }
    }
}

thread 1 started.
thread 2 started.
thread 2 ended.
thread 1 ended.

On running above example multiple times, 'thread 1 ended.' is always printed at the end. This thread has the lowest priority so it takes less CPU time when the two threads are competing, hence it is slower than 'thread 2' and the corresponding string is printed at the end.

Let's interchange the priorities:

        MyThread thread = new MyThread();
        thread.setName("thread 1");
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();

        Thread thread2 = new MyThread();
        thread2.setName("thread 2");
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread2.start();

thread 1 started.
thread 2 started.
thread 1 ended.
thread 2 ended.

This time 'thread 2 ended.' is always printed at the end.

Thread priority can be set between 1 to 10. Default is Thread.NORM_PRIORITY i.e. 5


Thread Priorities behavior might be different on different Operating systems. I ran above example on windows 10

........
OS Name:                   Microsoft Windows 10 Home
OS Version:                10.0.10586 N/A Build 10586
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Standalone Workstation
OS Build Type:             Multiprocessor Free
......
System Type:               x64-based PC
Processor(s):              1 Processor(s) Installed.
                           [01]: Intel64 Family 6 Model 71 Stepping 1 GenuineIntel ~1900 Mhz
BIOS Version:              American Megatrends Inc. E1793IMS.108, 6/11/2015
.......
Total Physical Memory:     16,299 MB
Available Physical Memory: 2,681 MB
Virtual Memory: Max Size:  24,085 MB
Virtual Memory: Available: 3,294 MB
......

Thread Priorities are used on competing threads and their behavior varies from OS to OS. We shouldn't make coding decisions based on thread priorities.



Example Project

Dependencies and Technologies Used :

  • JDK 1.8
  • Maven 3.0.4

Java Thread Priority Example Select All Download
  • java-thread-priority
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also