说到yield这里就涉及到线程优先级
setPriority方法在线程运行前就可以设置线程的优先级
最小为1最大为10,当然如果不设置就是5
public static void main(String[] args) throws Exception {
Thread t2 = new Thread((() -> {
for (int i = 0; i < 1000; i++) {
log.debug("t2线程执行===>"+i);
}
}), "t2");
Thread t3 = new Thread((() -> {
for (int i = 0; i < 1000; i++) {
log.debug(" t3线程执行===>"+i);
}
}), "t2");
t2.setPriority(1);
t3.setPriority(10);
t2.start();
t3.start();
}
我们设置t2的优先级为1(最小),t3优先级为10(最大)
发现果然是t3先运行完但是差距都不大
我们设置t3为6 ,t2为5,我们发现设置底的反而先执行完,说明优先级也不是完全靠谱的,它只是告诉了调度器那些的优先级高点,具体运行还得看调度器



