java 中的线程优先级的范围是1~10,默认的优先级是5。10最高。
MIN_PRIORITY 1
MAX_PRIORITY 10
NORM_PRIORITY 5
优先级高的获得cpu的几率更大些,不是优先级高的就先执行完,线程优先级随机特性
在java中,线程的优先级具有继承性,例如A线程启动B线程,则A和B的优先级是一样的
线程创建后,可通过调用setPriority()方法改变优先级。
1public class Test5 { 2 3 public static class TheadT extends Thread{ 4 @Override 5 public void run() { 6 while (true) { 7 System.out.println(Thread.currentThread().getName()); 8 } 9 } 10 } 11 12 public static void main(String[] args) { 13 Thread t1=new TheadT(); 14 t1.setName("t1"); 15 Thread t2=new TheadT(); 16 t2.setName("t2"); 17 18 t1.setPriority(Thread.MIN_PRIORITY); 19 t2.setPriority(Thread.MAX_PRIORITY); 20 21 t1.start(); 22 t2.start(); 23 } 24}