Change Thread Priority in Java

Java 8Object Oriented ProgrammingProgramming

A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called.

The thread priority determines when the processor is provided to the thread as well as other resources. It can be changed using the method setPriority() of class Thread.

A program that demonstrates changing the thread priorities using the method setPriority() in Java is given as follows:

Example

 Live Demo

public class ThreadDemo extends Thread {
   public void run() {
      System.out.println("Running...");
   }
   public static void main(String[] args) {
      ThreadDemo thread1 = new ThreadDemo();
      ThreadDemo thread2 = new ThreadDemo();
      ThreadDemo thread3 = new ThreadDemo();
      ThreadDemo thread4 = new ThreadDemo();
      ThreadDemo thread5 = new ThreadDemo();
      System.out.println("Default thread priority of Thread 1: " + thread1.getPriority());
      System.out.println("Default thread priority of Thread 2: " + thread2.getPriority());
      System.out.println("Default thread priority of Thread 3: " + thread3.getPriority());
      System.out.println("Default thread priority of Thread 4: " + thread4.getPriority());
      System.out.println("Default thread priority of Thread 5: " + thread5.getPriority());
      System.out.println("\nChanging the default thread priority using the setPriority() method");
      thread1.setPriority(7);
      thread2.setPriority(3);
      thread3.setPriority(9);
      thread4.setPriority(2);
      thread5.setPriority(8);
      System.out.println("\nChanged thread priority of Thread 1: " + thread1.getPriority());
      System.out.println("Changed thread priority of Thread 2: " + thread2.getPriority());
      System.out.println("Changed thread priority of Thread 3: " + thread3.getPriority());
      System.out.println("Changed thread priority of Thread 4: " + thread4.getPriority());
      System.out.println("Changed thread priority of Thread 5: " + thread5.getPriority());
      System.out.println("\n" + Thread.currentThread().getName());
      System.out.println("\nDefault thread priority of Main Thread: " + Thread.currentThread().getPriority());
      Thread.currentThread().setPriority(10);
      System.out.println("Changed thread priority of Main Thread: " + Thread.currentThread().getPriority());
   }
}

Output

Default thread priority of Thread 1: 5
Default thread priority of Thread 2: 5
Default thread priority of Thread 3: 5
Default thread priority of Thread 4: 5
Default thread priority of Thread 5: 5
Changing the default thread priority using the setPriority() method
Changed thread priority of Thread 1: 7
Changed thread priority of Thread 2: 3
Changed thread priority of Thread 3: 9
Changed thread priority of Thread 4: 2
Changed thread priority of Thread 5: 8
main
Default thread priority of Main Thread: 5
Changed thread priority of Main Thread: 10
raja
Published on 19-Feb-2019 06:25:43
Advertisements