/**
- The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1; //最小优先级
/**
- The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5; //默认优先级
/**
- The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10; //最大优先级
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
//如果新的优先级大于10或者小于1抛出异常
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
//设置优先级
setPriority0(priority = newPriority);
}
}
继承性
- Java线程的优先级有继承性, 比如A线程启动B线程, 则B的优先级与A的是一样的
public class MyThread1 extends Thread {
@Override
public void run() {
System.out.println(“我是MyThread1我的优先级是:” + this.getPriority());
MyThread2 thread2 = new MyThread2();
thread2.start();
}
}
class MyThread2 extends Thread {
@Override
public void run() {
System.out.println(“我是MyThread1我的优先级是:” + this.getPriority());
}
}
public class Run {
public static void main(String[] args) {
System.out.println(“我是main我的优先级是:” + Thread.currentThread().getPriority());
Thread.currentThread().setPriority(9);
System.out.println(“我是修改后的main我的优先级是:” + Thread.currentThread().getPriority());
MyThread1 thread1 = new MyThread1();
thread1.start();
}
}
我是main我的优先级是:5
我是修改后的main我的优先级是:9
我是MyThread1我的优先级是:9
我是MyThread1我的优先级是:9
Process finished with exit code 0
规则性
-
所谓规则性就是高优先级的线程在大多数情况下是优先执行完的. 但这并不代表优先级高的线程全部是优先执行完, 还会和代码的调用顺序有关, 除非有俩个线程的优先级差距很大, 这时就和代码的调用顺序无关了
-
例如下面代码
public class MyThread1 extends Thread {
@Override
public void run() {
this.setPriority(1);
System.out.println(“我是MyThread1我的优先级是:” + this.getPriority());
MyThread2 thread2 = new MyThread2();
thread2.start();
int i = 0;
while (true) {
i++;
System.out.println(“我是MyThread1=” + i);
}
}
}
class MyThread2 extends Thread {
@Override
public void run() {
this.setPriority(9);
System.out.println(“我是MyThread1我的优先级是:” + this.getPriority());
int i = 0;
while (true) {
i++;
System.out.println(“我是MyThread2=” + i);
}
}
}
- 明显是1先启动, 但是由于优先级差距较大, 所以他俩的执行就和代码调用顺序无关了
是MyThread1=71681
我是MyThread1=71682
我是MyThread1=71683
我是MyThread1=71684
我是MyThread1=71685
我是MyThread2=241867
我是MyThread2=241868
我是MyThread2=241869
我是MyThread2=241870
我是MyThread2=241871
我是MyThread2=241872
我是MyThread2=241873
我是MyThread2=241874
随机性
-
所谓随机性就是上面说到的如果俩个线程的优先级差距很小的话, 不一定优先级较高的那个线程每一都是先执行完
-
例如下面代码, 即使你比我较高, 而且你还是先启动, 但是咋俩的执行结果是差不多的
public class MyThread1 extends Thread {
@Override
public void run() {
this.setPriority(6);
System.out.println(“我是MyThread1我的优先级是:” + this.getPriority());
MyThread2 thread2 = new MyThread2();
thread2.start();
int i = 0;
while (true) {
i++;
System.out.println(“我是MyThread1=” + i);
}
}
}
class MyThread2 extends Thread {
@Override
public void run() {
this.setPriority(5);
System.out.println(“我是MyThread1我的优先级是:” + this.getPriority());
int i = 0;
while (true) {
i++;
System.out.println(“我是MyThread2=” + i);
}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
存中…(img-k2RrOgxG-1715715529725)]
[外链图片转存中…(img-GgRJrRGY-1715715529725)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!