Java多线程-线程的优先级

public final void setPriority(int newPriority) 更改线程的优先级。 

首先调用线程的 checkAccess 方法,且不带任何参数。这可能抛出 SecurityException。 在其他情况下,线程优先级被设定为指定的 newPriority 和该线程的线程组的最大允许优先级相比较小的一个。

参数:

newPriority - 要为线程设定的优先级 抛出: IllegalArgumentException - 如果优先级不在 MIN_PRIORITYMAX_PRIORITY 范围内。 SecurityException - 如果当前线程无法修改该线程。

注意:  

  • 线程优先级从1到10,1是最不重要的,10是最重要的。默认一个线程的优先级为5
  • 优先级高低代表的是的每次抢到进程资源的概率.
package com.gdzy.PriorityForThreadTest;

public class PriorityForThreadTest {

	public static void main(String[] args) {
		MyThread01 mt01 = new MyThread01();
		Thread td1 = new Thread(mt01);
		Thread td2 = Thread.currentThread();
		
		td1.setPriority(8); //将我的线程优先级设置为8
		td2.setPriority(9); //将main的线程优先级设置为9
		
		td1.start();  //启动我的线程
		
		for(int i=0; i<1000; i++){
			System.out.println("这是main的线程+++++"+i);
		}

	}

}

猜你喜欢

转载自blog.csdn.net/weixin_39646626/article/details/80728528