标签:Java Thread -- PRIORITY start setPriority new 多线程
四.线程优先级
java提供一个线程调度器监控程序中启动后进入就绪状态的所有线程,调度器按照优先级决定线程执行顺序。(优先级只表示获得调度的概率,并不是优先级低的不被调用,最终还是取决于CPU的调度)
优先级用数字表示,1~10,默认为5。
- Thread.MIN_PRIORITY = 1
- Thread.MAX_PRIORITY = 10
- Thread.NORM_PRIORITY = 5
通过get.Priority(),setPriority()获取和设置优先级。
public class Priority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
Priority p = new Priority();
Thread a = new Thread(p);
Thread b = new Thread(p);
Thread c = new Thread(p);
Thread d = new Thread(p);
Thread e = new Thread(p);
Thread f = new Thread(p);
a.setPriority(Thread.MIN_PRIORITY);
a.start();
b.setPriority(7);
b.start();
c.setPriority(Thread.MAX_PRIORITY);
c.start();
d.setPriority(2);
d.start();
e.setPriority(Thread.NORM_PRIORITY);
e.start();
f.setPriority(9);
f.start();
}
}
点击查看运行结果
main-->5
Thread-2-->10
Thread-5-->9
Thread-1-->7
Thread-4-->5
Thread-3-->2
Thread-0-->1
标签:Java,Thread,--,PRIORITY,start,setPriority,new,多线程 来源: https://www.cnblogs.com/tao-gak567/p/15361737.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。