Java supports multithreading, where multiple threads run concurrently and the Thread Scheduler decides their execution order. Each thread is assigned a priority (1–10) that influences scheduling but does not guarantee execution order.
Changing priority does not guarantee faster execution or immediate scheduling
Higher-priority threads are generally preferred by the scheduler
Actual execution order depends on the JVM and underlying OS
Java
classSimpleThreadextendsThread{publicvoidrun(){System.out.println(getName()+" is running with priority "+getPriority());}}publicclassGFG{publicstaticvoidmain(String[]args){SimpleThreadt1=newSimpleThread();SimpleThreadt2=newSimpleThread();t1.setName("HighPriorityThread");t2.setName("LowPriorityThread");t1.setPriority(Thread.MAX_PRIORITY);// 10t2.setPriority(Thread.MIN_PRIORITY);// 1t1.start();t2.start();}}
Output
LowPriorityThread is running with priority 1
HighPriorityThread is running with priority 10
Explanation:
Two threads are created: HighPriorityThread and LowPriorityThread.
HighPriorityThread is assigned maximum priority (10), and LowPriorityThread gets minimum priority (1).
When start() is called, both threads run concurrently.
Higher-priority threads may get more CPU time, but the actual execution order is not guaranteed because it depends on the JVM and the OS scheduler.
Thread Priority Constants
Java provides three constant values in the Thread class:
Thread.MIN_PRIORITY (1): Lowest possible priority for a thread.
Thread.NORM_PRIORITY (5): Default priority assigned to a thread.
Thread.MAX_PRIORITY (10): Highest possible priority for a thread.
Setting and Getting Thread Priority
We can use the following methods of the Thread class to manage thread priority:
setPriority(int newPriority): This method is used to set the priority of a thread.
getPriority(): This method is used to returns the current priority of the thread.
Thread-2 with priority 5
Thread-1 with priority 1
Thread-3 with priority 10
Explanation: The program creates three threads, assigns them different priorities (1, 5, and 10), and starts them using start(). Each thread runs concurrently and prints its name and priority, but the execution order is not fixed and depends on the scheduler.
Note: The output order may vary because thread scheduling depends on the JVM and the underlying operating system.
If multiple threads have the same priority, their execution order is decided by the thread scheduler. The example below demonstrates this, followed by an explanation of the output for better conceptual and practical understanding.
Java
classMyThreadextendsThread{publicvoidrun(){System.out.println(getName()+" is running with priority "+getPriority());}publicstaticvoidmain(String[]args){// Set main thread priorityThread.currentThread().setPriority(5);// Create two threadsMyThreadt1=newMyThread();MyThreadt2=newMyThread();// Both threads inherit main thread priority (5)System.out.println("t1 priority: "+t1.getPriority());System.out.println("t2 priority: "+t2.getPriority());// Start threadst1.start();t2.start();}}
Output
t1 priority: 5
t2 priority: 5
Thread-1 is running with priority 5
Thread-0 is running with priority 5
Explanation: The main thread sets its priority to 5, and both t1 and t2 inherit this priority when created. When start() is called, both threads run concurrently and print their details, and since they have the same priority, their execution order depends on the OS scheduler.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.