每一个 Java 线程都有一个优先级,这有助于操作系统确定线程调度的顺序。你可以获取和设置线程的优先级。Thread
类提供了用于处理线程优先级的方法和常量。
具有较高优先级的线程对于程序来说更为重要,并应在较低优先级的线程之前分配处理器时间。然而,线程优先级不能保证线程执行的顺序,并且非常依赖于平台。
Thread
类的内置属性常量
Java 线程优先级的范围是从 MIN_PRIORITY
(一个值为 1 的常量)到 MAX_PRIORITY
(一个值为 10 的常量)。默认情况下,每个线程被赋予 NORM_PRIORITY
(一个值为 5 的常量)的优先级。
-
MIN_PRIORITY
: 指定线程可以拥有的最低优先级。
-
NORM_PRIORITY
: 指定线程被分配的默认优先级。
-
MAX_PRIORITY
: 指定线程可以拥有的最高优先级。
线程优先级设置和获取方法
-
Thread.getPriority()
方法:此方法用于获取线程的优先级。
-
Thread.setPriority()
方法:此方法用于设置线程的优先级,它接受优先级值并使用给定的优先级更新现有的优先级。
Java 线程优先级示例
在这个例子中,我们展示了一个简单的单线程程序,在这里我们没有声明任何线程,并在程序执行中检查线程名称和优先级。
package com.tutorialspoint;
public class TestThread {
public void printName() {
System.out.println("Thread Name: " + Thread.currentThread().getName());
System.out.println("Thread Priority: " +Thread.currentThread().getPriority());
}
public static void main(String args[]) {
TestThread thread = new TestThread();
thread.printName();
}
}
输出
Thread Name: main
Thread Priority: 5
更多线程优先级示例
示例 1
在这个例子中,我们创建了一个扩展 Thread
类的 ThreadDemo
类。我们创建了三个线程。每个线程都被分配了一个优先级。在 run()
方法中,我们打印了优先级,并在输出中反映了线程执行的情况。
package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
}
public void run() {
System.out.println("Thread Name: " + Thread.currentThread().getName()
+ ", Thread Priority: " +Thread.currentThread().getPriority());
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void start () {
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
ThreadDemo thread3 = new ThreadDemo();
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.MIN_PRIORITY);
thread3.setPriority(Thread.NORM_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
输出
Thread Name: Thread-2, Thread Priority: 5
Thread Name: Thread-1, Thread Priority: 1
Thread Name: Thread-0, Thread Priority: 10
Thread: Thread-1, 4
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-0, 4
Thread: Thread-1, 2
Thread: Thread-2, 3
Thread: Thread-0, 3
Thread: Thread-0, 2
Thread: Thread-0, 1
Thread: Thread-2, 2
Thread: Thread-2, 1
Thread: Thread-1, 1
示例 2
在这个例子中,我们创建了一个扩展 Thread
类的 ThreadDemo
类。我们创建了三个线程。由于我们没有设置任何优先级,因此每个线程都有正常的优先级。在 run()
方法中,我们打印了优先级,并在输出中显示线程以任意顺序执行。
package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
}
public void run() {
System.out.println("Thread Name: " + Thread.currentThread().getName()
+ ", Thread Priority: " +Thread.currentThread().getPriority());
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void start () {
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
ThreadDemo thread3 = new ThreadDemo();
thread1.start();
thread2.start();
thread3.start();
}
}
输出
Thread Name: Thread-1, Thread Priority: 5
Thread Name: Thread-2, Thread Priority: 5
Thread Name: Thread-0, Thread Priority: 5
Thread: Thread-2, 4
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-0, 4
Thread: Thread-2, 2
Thread: Thread-1, 2
Thread: Thread-2, 1
Thread: Thread-0, 3
Thread: Thread-1, 1
Thread: Thread-0, 2
Thread: Thread-0, 1