java多线程优先级

多线程–优先级

常用方法 描述
setPriority(值) 设置优先级
getPriority(值) 获取优先级

值:

常量
NORM_PRIORITY 5 ( 默认)
MIN_PRIORITY 1
MAX_PRIORITY 10
public class YouXianJi {
	public static void main(String[] args) {
		YXJ yxj = new YXJ();
		Thread t1 = new Thread(yxj,"A");
		Thread t2 = new Thread(yxj,"B");
		Thread t3 = new Thread(yxj,"C");
		//设置优先级
		t1.setPriority(Thread.NORM_PRIORITY);
		t2.setPriority(Thread.MIN_PRIORITY);
		t3.setPriority(Thread.MAX_PRIORITY);
		//开启线程
		t1.start();
		t2.start();
		t3.start();
	}
}
class YXJ implements Runnable{
	public void run() {
		System.out.println(Thread.currentThread().getName()+"---执行"+Thread.currentThread().getPriority());
	}
}

注意咯,优先级不是越高执行的概率就越高。(不信?你试试!)

发布了22 篇原创文章 · 获赞 16 · 访问量 1614

猜你喜欢

转载自blog.csdn.net/qq_43098197/article/details/96868989