线程优先级
线程的优先级指的是,线程的优先级越高越有可能先执行,但仅仅是有可能而已。
/**
*设置优先级
**/
public final void setPriority(int newPriority)
/**
*取得优先级
**/
public final int getPriority()
对于优先级设置的内容可以通过Thread类的几个常量来决定:
- 最高优先级:public final static int MAX_PRIORITY = 10;
- 中等优先级:public final static int NORM_PRIORITY = 5;
- 最低优先级:public final static int MIN_PRIORITY = 1;
/**设置优先级,获取主线程的优先级**/
class MyThread1 implements Runnable{
@Override
public void run(){
Thread t = Thread.currentThread();//创建一个当前线程的实例化对象
System.out.println("当前线程"+t.getName()+"的优先级是:"+t.getPriority());
}
}
public class SetPriority {
public static void main(String[] args) {
/*获取主方法的线程优先级*/
Thread threadA = new Thread(() -> {
System.out.println(Thread.currentThread().getName(