Java中线程优先级的重要性?
在多线程应用程序中,每个线程都分配有一个优先级。线程调度程序根据线程的优先级将处理器分配给该线程,即,优先级最高的线程首先分配给处理器,依此类推。值为'5'的线程的默认优先级。我们可以使用Thread类的getPriority() 方法获得线程的优先级。
Thread类中为线程优先级定义的三个静态值
MAX_PRIORITY
这是最大线程优先级,值为10。
NORM_PRIORITY
这是默认的线程优先级,值为5。
MIN_PRIORITY
这是最小线程优先级,值为1。
语法
public final int getPriority()
示例
public class ThreadPriorityTest extends Thread {
public static void main(String[]args) {
ThreadPriorityTest thread1 = new ThreadPriorityTest();
ThreadPriorityTest thread2 = new ThreadPriorityTest();
ThreadPriorityTest thread3 = new ThreadPriorityTest();
System.out.println("Default thread priority of thread1: " + thread1.getPriority());
System.out.println("Default thread priority of thread2: " + thread2.getPriority());
System.out.println("Default thread priority of thread3: " + thread3.getPriority());
thread1.setPriority(8);
thread2.setPriority(3);
thread3.setPriority(6);
System.out.println("New thread priority of thread1: " + thread1.getPriority());
System.out.println("New thread priority of thread2: " + thread2.getPriority());
System.out.println("New thread priority of thread3: " + thread3.getPriority());
}
}输出结果
Default thread priority of thread1: 5 Default thread priority of thread2: 5 Default thread priority of thread3: 5 New thread priority of thread1: 8 New thread priority of thread2: 3 New thread priority of thread3: 6
热门推荐
10 关于扬州的祝福语简短
11 下雪路滑祝福语简短
12 恋爱祝福语大全简短搞笑
13 工人生日祝福语简短
14 鼓励孩子小考祝福语简短
15 中秋祝福语简短版大全
16 哥哥病了祝福语大全简短
17 祝福语简短七夕
18 恭喜新郎父母简短祝福语