在Thread类里面提供以下两个方法:
·设置优先级:public final void setPriority(int newPriority);
·取得优先级:public final int getPriority();
设置和取得优先级都是使用int数据类型,对于此内容有三种取值:
·最高优先级:public static void int MAX_PRIORITY,10;
·中等优先级:public static void int NORM_PRIORITY,5;
·最低优先级:public static void int MIN_PRIORITY,1;
范例1:
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"->"+i);
}
}
}
public class Demo {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt, "线程A");
Thread t2 = new Thread(mt, "线程B");
Thread t3 = new Thread(mt, "线程C");
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}