每个 Java 线程都有一个优先级,可帮助操作系统确定线程的调度顺序。
Java 线程优先级在以下范围内介于 MIN_PRIORITY(常量 1)和 MAX_PRIORITY(常量 10)之间。默认情况下,每个线程都被赋予优先级 NORM_PRIORITY(常数为 5)。
具有较高优先级的线程对于程序来说更重要,并且应该在较低优先级的线程之前分配处理器时间。但是,线程优先级不能保证线程执行的顺序,并且在很大程度上依赖于平台。
示例 1
在此示例中,我们将展示一个简单的单线程程序,其中我们使用'没有声明任何线程并在程序执行中检查线程名称和优先级。
package com.yxjc123;
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();//默认打印优先级5
}
}
输出
Thread Name: main
Thread Priority: 5
示例 2
在此示例中,我们创建了一个继承 Thread 类的 ThreadDemo 类。我们创建了三个线程。每个线程都被分配一个优先级。在 run() 方法中,我们打印优先级,在输出中,它反映在线程执行中。
package com.yxjc123;
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) {
// TODO 自动生成的 catch 块
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
示例 3
In在这个例子中,我们创建了一个继承 Thread 类的 ThreadDemo 类。我们创建了三个线程。由于我们没有设置任何优先级,因此每个线程都有一个正常的优先级。在 run() 方法中,我们打印优先级,在输出中,线程以任意顺序执行。
package com.yxjc123;
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) {
// TODO 自动生成的 catch 块
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