线程的其他知识:
线程优先级:
一般线程的优先级越高,得以运行的机会越多。
线程优先级常量:
Thread有3个publis tatic final int 成员变量,它们表示可以传递给setPriority()的优先级值的范围。
•Thread.MAX_PRIORITY
Thread.MAX_PRIORITY是最高的线程规划优先级,在特定的JVM中传递给setPriority()。一般来说,这个值为10。
•Thread.MIN_PRIORITY
Thread.MIN_PRIORITY是最低的线程规划优先级,在特定的JVM中传递给setPriority()。一般来说,这个值为1。
•Thread.NORM_PRIORITY
Thread.NORM_PRIORITY是一个不太高,也不是太低的线程规划优先级,也可以传递给setPriority()。一般来说,这个值为5。
判断当前优先级:getPriority()
Thread的getPriority()方法返回一个int,它表示线程的当前优先级。一般来说,线程的优先级在它的生存期内不会改变,但也可以改变。
GetPriority.java ?? 使用getPriority()
public class GetPriority
extends Object {
private static Runnable makeRunnable() {
Runnable r = new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
Thread t = Thread.currentThread();
System.out.println(
"in run() - priority=" +
t.getPriority() +
", name=" + t.getName());
try {
Thread.sleep(2000);
}
catch (InterruptedException x) {
// ignore
}
}
}
};
return r;
}
public static void main(String[] args) {
System.out.println(
"in main() - Thread.currentThread().getPriority()=" +
Thread.currentThread().getPriority());
System.out.println(
"in main() - Thread.currentThread().getName()=" +






