线程的优先级,默认的是5,
最大的优先级是10
最小的优先级是1
我们测试将后执行的thread1优先级设置为10,先执行的thread2优先级设置为1,最后得出结果,优先级高thread1的获取cpu的机会更大,例如优先级为10的thread1的线程执行到了第3306665次了,thread2才执行到1014520次,几乎是thread2的执行的3倍的速度
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000000; i++) {
Log.d("kodulf", "thread1"+" "+i);
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000000; i++) {
Log.d("kodulf", "thread2"+" "+i);
}
}
});
thread2.setPriority(1);
thread2.start();
thread1.setPriority(10);
thread1.start();
D/kodulf: thread2 1014519
D/kodulf: thread1 3306664
D/kodulf: thread2 1014520
D/kodulf: thread1 3306665