线程中优先级分为1~10这10个等级, 如果你设置的大于10或者小于1, 则JDK会抛出异常throw new IllegalArgumentException()
源码分析
/**
* The minimum priority that a thread can have.
*/publicfinalstaticint MIN_PRIORITY =1;//最小优先级/**
* The default priority that is assigned to a thread.
*/publicfinalstaticint NORM_PRIORITY =5;//默认优先级/**
* The maximum priority that a thread can have.
*/publicfinalstaticint MAX_PRIORITY =10;//最大优先级publicfinalvoidsetPriority(int newPriority){
ThreadGroup g;checkAccess();//如果新的优先级大于10或者小于1抛出异常if(newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY){thrownewIllegalArgumentException();}if((g =getThreadGroup())!= null){if(newPriority > g.getMaxPriority()){
newPriority = g.getMaxPriority();}//设置优先级setPriority0(priority = newPriority);}}
文章目录线程的优先级源码分析继承性规则性随机性守护线程线程的优先级在操作系统中, 线程可以划分优先级, 优先级较高的会先得到CPU的资源较多, 也就是会优先执行优先级高的线程中的任务通过设置优先级有助于帮"线程规划期"确定下一次选择哪个线程来优先执行线程中优先级分为1~10这10个等级, 如果你设置的大于10或者小于1, 则JDK会抛出异常throw new IllegalArgumentException()源码分析/** * The minimum priority that