Home >Java >javaTutorial >Java thread priority in multithreading

Java thread priority in multithreading

王林
王林forward
2023-09-06 14:21:06892browse

Java thread priority in multithreading

In the case of multi-threading, the thread scheduler allocates threads to specific processes based on different conditions. their priorities. Java threads have pre-assigned priorities. In addition, java virtual The machine can also assign priorities to threads or specify them explicitly by the programmer. The range is Thread priority has a value between 1 and 10 (inclusive). three static variables Related to priorities are -

  • MAX_PRIORITY - the maximum priority a thread has, the default value is 10.

  • NORM_PRIORITY - The default priority the thread has, the default value is 5.

  • MIN_PRIORITY - The minimum priority a thread has, default is 1.

The "getPriority()" method in Java helps to return the thread priority bound as a value.

The "setPriority()" method changes the priority value of a given thread. it throws IllegalArgumentException occurs when the thread priority is less than 1 or greater than 10.

Example

Real-time demonstration

import java.lang.*;
public class Demo extends Thread{
   public void run(){
      System.out.println("Now, inside the run method");
   }
   public static void main(String[]args){
      Demo my_thr_1 = new Demo();
      Demo my_thr_2 = new Demo();
      System.out.println("The thread priority of first thread is : " + my_thr_1.getPriority());
      System.out.println("The thread priority of first thread is : " +       my_thr_2.getPriority());
      my_thr_1.setPriority(5);
      my_thr_2.setPriority(3);
      System.out.println("The thread priority of first thread is : " +    my_thr_1.getPriority());
      System.out.println("The thread priority of first thread is : " + my_thr_2.getPriority());
      System.out.print(Thread.currentThread().getName());
      System.out.println("The thread priority of main thread is : " +
      Thread.currentThread().getPriority());
      Thread.currentThread().setPriority(10);
      System.out.println("The thread priority of main thread is : " +
      Thread.currentThread().getPriority());
   }
}

Output

The thread priority of first thread is : 5
The thread priority of first thread is : 5
The thread priority of first thread is : 5
The thread priority of first thread is : 3
The thread priority of main thread is : 5
The thread priority of main thread is : 10

The class named Demo inherits from the base class Thread. Function 'run' is defined and relevant The message is defined. In the main function, two instances of the Demo class are created and they are The priority is found by calling the function "getPriority".

They are printed on the console. Next, assign a priority to the Demo instance using: ‘Set priority’ function. The output is displayed on the console. Print the name of the thread Displayed on the screen with the help of the "getName" function.

The above is the detailed content of Java thread priority in multithreading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete