Java中的最小和最大优先级线程
线程优先级确定何时将处理器提供给线程以及其他资源。可以使用setPriority()Thread类的方法进行更改。
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("\nThe 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("\n" + 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
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便