Java线程优先级
线程的优先级:
1-min
10-max
5-normal
优先级只是可能会优先占据资源,不会一定。
class ThRun implements Runnable{
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+":"+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDemo03 {
public static void main(String[] args) {
Thread t1=new Thread(new ThRun(),"A");
Thread t2=new Thread(new ThRun(),"B");
Thread t3=new Thread(new ThRun(),"C");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
输出:
B:0 C:0 A:0 C:1 B:1 A:1 A:2 B:2 C:2 C:3 B:3 A:3 B:4 C:4 A:4

浙公网安备 33010602011771号