- 它是一个静态方法,表示当前线程让出CPU的执行权,让出去之后,会依旧和其他线程去争夺 CPU的执行权
- 如果有多个线程,那么多个线程继续抢夺CPU的执行权
public class TheadDemo08 {
public static void main(String[] args) {
YieldThread t1 = new YieldThread();
YieldThread t2 = new YieldThread();
t1.setName("哥哥");
t2.setName("弟弟");
t1.start();
t2.start();
}
}
class YieldThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(getName() + ":" + i);
Thread.yield();
}
}
}