Thread.currentThread().getName());
Thread threadA = new Thread(makeRunnable(), "threadA");
threadA.start();
try {
Thread.sleep(3000);
}
catch (InterruptedException x) {}
System.out.println("in main() - threadA.getPriority()=" +
threadA.getPriority());
}
}
更改线程的优先级:setPriority()
Thread的setPriority()方法带有一个int作为参数。setPriority()方法可以在启动线程前调用,也可以在运行的时候调用。
SetPriority.java ?? 用setPriority()更改线程的优先级
public class SetPriority
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) {
Thread threadA = new Thread(makeRunnable(), "threadA");
threadA.setPriority(Thread.MAX_PRIORITY);
threadA.start();
Thread threadB = new Thread(makeRunnable(), "threadB");
threadB.setPriority(2);