1.Java可以通过设置优先级,来决定线程的运行优先级
2.通过 setPriority(int newPriority)来设置优先级,其中函数里面写为Thread.NORM_PRIORITY + n.最高级为5,若超过,则会报IllegalArgumentException 异常
代码:
public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 5);
t1.start();
t2.start();
}
}
class T1 implements Runnable {
public void run() {
for(int i=0;i<1000;i++) {
System.out.println("T1 " + i);
}
}
}
class T2 implements Runnable {
public void run() {
for(int i=0;i<1000;i++) {
System.out.println("T2 " + i);
}
}
}