Java 中的最低和最高优先级线程
线程优先级决定了处理器如何提供给线程以及其他资源。它可以使用 Thread 类的 setPriority() 方法更改。
Java 中的线程优先级有三个静态变量,即 MIN_PRIORITY、MAX_PRIORITY 和 NORM_PRIORITY。这些变量的值分别为 1、10 和 5。
如下所示给出了一个展示这一点的程序
示例
public class ThreadDemo extends Thread {
public void run() {
System.out.println("Running...");
}
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
System.out.println("Default thread priority of Thread 1: " + thread1.getPriority());
System.out.println("Default thread priority of Thread 2: " + thread2.getPriority());
thread1.setPriority(MAX_PRIORITY);
thread2.setPriority(MIN_PRIORITY);
System.out.println("
The maximum thread priority of Thread 1 is: " + thread1.getPriority());
System.out.println("The minimum thread priority of Thread 2 is: " + thread2.getPriority());
System.out.println("
" + Thread.currentThread().getName());
System.out.println("Default thread priority of Main Thread: " + Thread.currentThread().getPriority());
Thread.currentThread().setPriority(MAX_PRIORITY);
System.out.println("The maximum thread priority of Main Thread is: " + Thread.currentThread().getPriority());
}
}输出
Default thread priority of Thread 1: 5 Default thread priority of Thread 2: 5 The maximum thread priority of Thread 1 is: 10 The minimum thread priority of Thread 2 is: 1 main Default thread priority of Main Thread: 5 The maximum thread priority of Main Thread is: 10
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP