java 中的線程優先級的範圍是1~10,預設的優先級是5。“高優先級線程”會優先于“低優先級線程”執行。
例子:
package com.ming.thread.threadpriority;
public class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "(" + Thread.currentThread().getPriority() + ")"+ ", loop " + i);
}
}
}
package com.ming.thread.threadpriority;
/**
* 線程的優先級的值是1--10
* @author ming
*
*/
public class Run {
public static void main(String[] args) {
MyThread t1=new MyThread("t1");
MyThread t2=new MyThread("t2");
MyThread t3=new MyThread("t3");
t1.setPriority(6);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(1);
t1.start();
t2.start();
t3.start();
}
}