线程的优先级

1.优先级顺序设置(有可能影响线程的执行顺序,不会一定影响)

  1-MIN_PRIOPITY

  10-MAX_PRIORITY-----会提高当前线程的执行速度,有很大概率抢到cpu运行资源,但不一定

  5-NORM_PRIORITY

  如果什么都不设置默认值是5

2.ThreadDemo04.java

package thread;
class ThRun implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+":"+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}
public class ThreadDemo04 {
public static void main(String[] args) {
//通过匿名方式创建线程对象,第二个参数为线程的名字
Thread t1=new Thread(new ThRun(),"A");
Thread t2=new Thread(new ThRun(),"B");
Thread t3=new Thread(new ThRun(),"C");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}

原文地址:https://www.cnblogs.com/curedfisher/p/11975378.html