Home > Java > Java Tutorial > body text

Java thread priority in multithreading

王林
Release: 2023-09-06 14:21:06
forward
691 people have browsed it

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());
   }
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!