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
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短