Java Examples - Setting the priority

Advertisements


Problem Description:

How to set the priority of a thread?

Solution:

Following example how to set the priority of a thread with the help of.

public class Main {
   public static void main(String[] args) 
   throws Exception {
      Thread thread1 = new Thread(new TestThread(1));
      Thread thread2 = new Thread(new TestThread(2));
      thread1.setPriority(Thread.MAX_PRIORITY);
      thread2.setPriority(Thread.MIN_PRIORITY);
      thread1.start();
      thread2.start();
      thread1.join();
      thread2.join();
      System.out.println("The priority has been set.");
   }
}

Result:

The above code sample will produce the following result.

The priority has been set.


Advertisements
Advertisements