龙空技术网

多线程的优先级

滴滴滴程序员 62

前言:

今天看官们对“线程的优先级别”都比较珍视,同学们都想要知道一些“线程的优先级别”的相关资讯。那么小编也在网上网罗了一些对于“线程的优先级别””的相关知识,希望小伙伴们能喜欢,各位老铁们快快来了解一下吧!

讲一下线程的优先级

在多个线程同时执行时,线程的调度器会根据优先级,优先调用级别高的线程(这里强调的是优先调用,不代表绝对的先后顺序,只是优先调用级别高的概率比较大),

package cn.jd.state;/* * 线程的优先级,优先级的取值范围是数字1-10 * 在API里面提供了3个常量供我们操作 * 1.NORM_PRIORITY  所有线程默认是5 * 2.MIN_PRIORITY     1 * 3.MAX_PRIORITY    10 * 优先级代表的是概率事件,不代表绝对的先后顺序 */public class PriorityTest {	public static void main(String[] args) {//		System.out.println(Thread.currentThread().getPriority());		Test t=new Test();		Thread t1=new Thread(t,"1");		Thread t2=new Thread(t,"2");		Thread t3=new Thread(t,"3");		Thread t4=new Thread(t,"4");		Thread t5=new Thread(t,"5");		Thread t6=new Thread(t,"6");		//设置优先级一定要在启动之前		t1.setPriority(Thread.MAX_PRIORITY);		t2.setPriority(Thread.MAX_PRIORITY);		t3.setPriority(Thread.MIN_PRIORITY);		t4.setPriority(Thread.MIN_PRIORITY);		t5.setPriority(Thread.MIN_PRIORITY);		t6.setPriority(Thread.MIN_PRIORITY);		t1.start();		t2.start();		t3.start();		t4.start();		t5.start();		t6.start();					}}class Test implements Runnable{	@Override	public void run() {		System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());//		Thread.yield();//礼让一下还会发送改变			}	}

视频加载中...

标签: #线程的优先级别