首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

上干货:Java编程教程——线程的优先级

每个线程都有一个优先级。优先级用1到10之间的数字表示。在大多数情况下,线程调度器根据线程的优先级进行调度(称为抢占式调度)。但是这不是有保证的,因为它取决于JVM规范选择的调度方式。请注意,不仅JVM,Java程序员还可以在Java程序中显式地为线程分配优先级。

# 设置器和获取器方法的线程优先级

让我们讨论线程优先级的设置器和获取器方法。

public final int getPriority(): java.lang.Thread.getPriority()方法返回给定线程的优先级。

public final void setPriority(int newPriority): java.lang.Thread.setPriority()方法将线程的优先级更新或分配给newPriority。如果值newPriority超出范围(1到10),则该方法会抛出IllegalArgumentException。

Thread类中定义了3个常量:

public static int MIN_PRIORITY

public static int NORM_PRIORITY

public static int MAX_PRIORITY

线程的默认优先级是5(NORM_PRIORITY)。MIN_PRIORITY的值为1,MAX_PRIORITY的值为10。

# 线程优先级的示例:

文件名:ThreadPriorityExample.java

// 导入需要的类 import java.lang.*; public class ThreadPriorityExample extends Thread { // 方法一 // 每当线程调用 start() 方法时 // run() 方法被调用 public void run() { // 打印语句 System.out.println("Inside the run() method"); } //主要方法 public static void main(String argvs[]) { // 在 ThreadPriorityExample 类的帮助下创建线程 ThreadPriorityExample th1 = new ThreadPriorityExample(); ThreadPriorityExample th2 = new ThreadPriorityExample(); ThreadPriorityExample th3 = new ThreadPriorityExample(); // 我们没有提到线程的优先级。// 因此线程的优先级为5,默认值 // 第一个线程 // 显示线程的优先级 // 使用 getPriority() 方法 System.out.println("Priority of the thread th1 is : " + th1.getPriority()); // 第二个线程 // 显示线程的优先级 System.out.println("Priority of the thread th2 is : " + th2.getPriority()); // 第三个线程 // // 显示线程的优先级 System.out.println("Priority of the thread th2 is : " + th2.getPriority()); // 设置以上线程的优先级 // 传递整数参数 th1.setPriority(6); th2.setPriority(3); th3.setPriority(9); // 6 System.out.println("Priority of the thread th1 is : " + th1.getPriority()); // 3 System.out.println("Priority of the thread th2 is : " + th2.getPriority()); // 9 System.out.println("Priority of the thread th3 is : " + th3.getPriority()); // 主线程 // 显示当前执行线程的名称 System.out.println("Currently Executing The Thread : " + Thread.currentThread().getName()); System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority()); // 主线程的优先级现在是10 Thread.currentThread().setPriority(10); System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority()); } }

输出:

Priority of the thread th1 is : 5Priority of the thread th2 is : 5Priority of the thread th2 is : 5Priority of the thread th1 is : 6Priority of the thread th2 is : 3Priority of the thread th3 is : 9Currently Executing The Thread : mainPriority of the main thread is : 5Priority of the main thread is : 10

我们知道,在线程执行时,优先级高的线程会优先于优先级低的线程。然而,还有其他情况下两个线程可能具有相同的优先级。所有关于线程的处理工作都由Java线程调度器完成。请参考以下示例,以了解如果两个线程具有相同优先级会发生什么。

文件名:ThreadPriorityExample1.java

// 导入 java.lang 包 import java.lang.*; public class ThreadPriorityExample1 extends Thread { // 方法一 // 每当线程调用 start() 方法时 // run() 方法被调用 public void run() { // 打印语句 System.out.println("Inside the run() method"); } // 主要方法 public static void main(String argvs[]) { // 现在,主线程的优先级设置为 7 Thread.currentThread().setPriority(7); // 获取当前线程 // 使用 currentThread() 方法 // 显示主线程优先级 // 使用 Thread 类的 getPriority() 方法 System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority()); // 通过创建 ThreadPriorityExample1 类的对象来创建线程 ThreadPriorityExample1 ThreadPriorityExample1 th1 = new ThreadPriorityExample1(); // th1 线程是主线程的子线程 // 因此,第 1 个线程也获得优先级 7 // 显示当前线程的优先级 System.out.println("Priority of the thread th1 is : " + th1.getPriority()); } }

输出:

Priority of the main thread is : 7Priority of the thread th1 is : 7

说明:如果有两个具有相同优先级的线程,则无法预测哪个线程将有机会首先执行。执行取决于线程调度器的算法(先来先服务,循环调度等)。

# IllegalArgumentException的示例

我们知道,如果方法getPriority()的参数newPriority的值超出范围(1到10),则会得到IllegalArgumentException。让我们通过一个示例来观察同样的情况。

文件名:IllegalArgumentException.java

// 导入 java.lang 包 import java.lang.*; public class IllegalArgumentException extends Thread { //主要方法 public static void main(String argvs[]) { // 现在,主线程的优先级设置为 17,大于 10 Thread.currentThread().setPriority(17); // 获取当前线程 // 使用 currentThread() 方法 // 显示主线程优先级 // 使用 Thread 类的 getPriority() 方法 System.out.println("Priority of the main thread is : " + Thread.currentThread().getPriority()); } }

当我们执行以上程序时,会得到以下异常:

Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.lang.Thread.setPriority(Thread.java:1141) at IllegalArgumentException.main(IllegalArgumentException.java:12)

华清远见

让众多学子实现IT梦

实现高薪梦想

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OT4lyjfq9Kq6PFrdcWYCJptQ0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com