线程的优先级
线程优先级:getPriority() setPriority(int x)
- Java提供了一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度那个线程来执行。
- 线程的优先级用数字来表示,范围1~10。
- Thread.MIN_PRIORITY = 1; 最小优先级
- Thread.MAX_PRIORITY = 10; 最大优先级
- Thread.NORM_PRIORITY = 5; 默认优先级(main方法)
- 使用一下方式改变或获取优先级
- getPriority()
- setPriority(int x)
- 优先级的设定建议在start()调度前
- 优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用了,这都是看CPU的调度。
Code
// 线程方法体
public class ThreadDemo implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread()