Priority of a Thread in Java – Java中线程的优先级

最后修改: 2021年 9月 16日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll discuss how the Java thread scheduler executes threads on a priority basis. Additionally, we’ll cover the types of thread priorities in Java.

在本教程中,我们将讨论Java线程调度器如何在优先级基础上执行线程。此外,我们还将介绍Java中线程优先级的类型。

2. Types of Priority

2.优先权的类型

In Java, a thread’s priority is an integer in the range 1 to 10. The larger the integer, the higher the priority. The thread scheduler uses this integer from each thread to determine which one should be allowed to execute. The Thread class defines three types of priorities:

在Java中,线程的优先级是一个1到10的整数。整数越大,优先级就越高。线程调度器使用每个线程的这个整数来决定哪个线程应该被允许执行。Thread类定义了三种类型的优先级

  • Minimum priority
  • Normal priority
  • Maximum priority

The Thread class defines these priority types as constants MIN_PRIORITY, NORM_PRIORITY, and MAX_PRIORITY, with values 1, 5, and 10, respectively. NORM_PRIORITY is the default priority for a new Thread.

Thread类将这些优先级类型定义为常量MIN_PRIORITYNORM_PRIORITYMAX_PRIORITY,值分别为1、5和10。NORM_PRIORITY是一个新线程的默认优先级

3. Overview of Thread Execution

3.线程执行的概述

The JVM supports a scheduling algorithm called fixed-priority pre-emptive scheduling. All Java threads have a priority, and the JVM serves the one with the highest priority first.

JVM支持一种叫做固定优先级抢占式调度的调度算法。所有的Java线程都有一个优先级,JVM首先为具有最高优先级的线程服务。

When we create a Thread, it inherits its default priority. When multiple threads are ready to execute, the JVM selects and executes the Runnable thread that has the highest priority. If this thread stops or becomes not runnable, the lower-priority threads will execute. In case two threads have the same priority, the JVM will execute them in FIFO order.

当我们创建一个线程时,它将继承其默认的优先级。当多个线程准备执行时,JVM会选择并执行具有最高优先级的Runnable线程。如果这个线程停止或变得不可运行,那么优先级较低的线程将执行。如果两个线程具有相同的优先级,JVM将按FIFO顺序执行它们

There are two scenarios that can cause a different thread to run:

有两种情况会导致不同的线程运行。

  • A thread with higher priority than the current thread becomes runnable
  • The current thread exits the runnable state or yields (temporarily pause and allow other threads)

In general, at any time, the highest priority thread is running. But sometimes, the thread scheduler might choose low-priority threads for execution to avoid starvation.

一般来说,在任何时候,最高优先级的线程都在运行。但有时,线程调度器可能会选择低优先级的线程来执行,以避免饥饿

4. Knowing and Changing a Thread’s Priority

4.了解和改变线程的优先级

Java’s Thread class provides methods for checking the thread’s priority and for modifying it. The getPriority() instance method returns the integer that represents its priority. The setPriority() instance method takes an integer between 1 and 10 for changing the thread’s priority. If we pass a value outside the 1-10 range, the method will throw an error.

Java的Thread类提供了检查线程的优先级和修改它的方法。getPriority() 实例方法返回代表其优先级的整数。setPriority()实例方法接受1到10之间的整数,用于改变线程的优先级。如果我们传递一个超出1-10范围的值,该方法将抛出一个错误。

5. Conclusion

5.总结

In this short article, we looked at how multiple threads are executed in Java on a priority basis using the pre-emptive scheduling algorithm. We further examined the priority range and the default thread priority. Also, we analyzed Java methods for checking a thread’s priority and manipulating it if necessary.

在这篇短文中,我们研究了在Java中如何使用抢占式调度算法在优先级基础上执行多个线程。我们进一步研究了优先级范围和默认的线程优先级。此外,我们还分析了检查线程的优先级并在必要时对其进行操作的Java方法。