package A_ShangGuiGu.Thread.ThreadDemo;标签:java,Thread,currentThread,Priority,setPriority,线程,优先级 From: https://www.cnblogs.com/zhazhawei906/p/16815930.html
/**
* 优先级
* 1.MAX_Priority-----最大优先级为10
* 2.MIN_Priority-----最小优先级为1
* 3.NORM_Priority----默认优先级5
* 4.getPriority------获取当前线程的优先级
* 5.setPriority------设置当前线程的优先级
* 6.设置优先级必须在线程执行之前设置。
* 说明:优先级高的线程抢占优先级低的线程的执行权,只是概率抢占。并不一定百分百成功。
* 高优先级的线程只是高概率被执行而已。
*/
class Max_Thread extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+":"+i);
}
}
}
public class ThreadDemo03 {
public static void main(String[] args) {
Max_Thread max = new Max_Thread();
max.setPriority(Thread.MAX_PRIORITY);
max.start();
Thread.currentThread().setPriority(2);
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+":"+i);
}
}
}