個線程來執行
·線程的優先級用數字表示,範圍從1到10,一個線程的缺省優先級是5
Thread.MIN_PRIORITY=1
Thread.MAX_PRIORITY=10
Thread.NORM_PRIORITY=5
使用下述線方法獲得或設置線程對象的優先級。
int getPriority();
void setPriority(int newPriority);
提高線程的優先級可以使得線程優先行完
舉例:
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+3);
t1.start();
t2.start();
}
}
class T1 implements Runnable {
public void run(){
for(int i=0;i<50;i++) {
System.out.println("T1: "+i);
}
}
}
class T2 implements Runnable {
public void run(){
for(int i=0;i<50;i++) {
System.out.println("T2: "+i);
}
}
}