Java多线程-线程的优先级

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

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

参数:

newPriority - 要为线程设定的优先级 

 * Thread.MAX_PRIORITY表示线程的最大优先级
 * Thread.MIN_PRIORITY表示线程的最小优先级
 * Thread.NORM_PRIORITY表示线程的默认优先级

抛出:

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);
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值