setPriority(int n)
优先级 n的范围是 1~10 数字越大 优先级越高
这里。。是 cpu 还是随机进行来回切换
只不过 稍微对优先级高的 进行了照顾 不是确保优先级高的肯定先完成
class Demo implements Runnable
{
public void run() {
for(int i=0;i<10;i++)
System.out.println(Thread.currentThread()+"..."+i);
}
}
public class Main {
public static void main(String[] args) throws Exception
{
Demo d1=new Demo();
Demo d2=new Demo();
Demo d3=new Demo();
Thread t1=new Thread(d1);
Thread t2=new Thread(d2);
Thread t3=new Thread(d3);
//范围 1~10
t3.setPriority(10);
t1.setPriority(5);
t2.setPriority(1);
t1.start();
t2.start();
t3.start();
}
}