admin管理员组文章数量:1624351
线程的优先级(优先级(priority)要设置在.start之前)
- 线程的默认优先级为5(例如主线程main的),最低的优先级为1,最大的优先级为10.其中若是大于最大的优先级10或者小于最小的优先级1。他会抛出异常不会执行
- 优先级低cpu可能会发生先与优先级高的调度
还是要看cpu心情(麻了)
package org.example.threadpriority;
public class TestPriority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
Priority priority = new Priority();
Thread thread1 = new Thread(priority);
Thread thread2 = new Thread(priority);
Thread thread3 = new Thread(priority);
Thread thread4 = new Thread(priority);
Thread thread5 = new Thread(priority);
Thread thread6 = new Thread(priority);
thread1.start();
thread2.setPriority(1);
thread2.start();
thread3.setPriority(4);
thread3.start();
thread4.setPriority(Thread.MAX_PRIORITY);
thread4.start();
// thread5.setPriority(11);
thread5.setPriority(8);
thread5.start();
// thread6.setPriority(-1);
thread6.setPriority(6);
thread6.start();
}
}
class Priority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
}
}
版权声明:本文标题:13、线程的优先级(优先级(priority)要设置在.start之前) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728896105a1178428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论