一:看程序
public class MyThread1 extends Thread{
MyThread1(String name){
super(name);
}
@Override
public void run() {
for(int i = 0; i < 10000 ; i++){
System.out.println(getName()+": "+i);
}
}
}
public class MyThread2 extends Thread{
MyThread2(String name){
super(name);
}
@Override
public void run() {
for(int i = 0; i < 10000 ; i++){
System.out.println(getName()+": "+i);
}
}
}
public class Test {
public static void main(String[] args) {
MyThread1 t1 = new MyThread1("t1");
MyThread1 t2 = new MyThread1("t2");
t1.setPriority(Thread.NORM_PRIORITY + 3);
t1.start();
t2.start();
}
}
二:分析
t1.setPriority(Thread.NORM_PRIORITY + 3); 增加线程t1的优先级
执行可以发现,优先输出线程t1。