java中的线程优先级_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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值