线程礼让
- 线程礼让,让当前正在执行的线程暂停,但不阻塞
- 将线程从运行状态转为就绪状态
- 让cpu重新调度,礼让不一定成功!看cpu心情
public class TestYield {
public static void main(String[] args) {
thread thread = new thread();
new Thread(thread,"xiaoming").start();
new Thread(thread,"xiaohong").start();
}
}
class thread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"开始");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"结束");
}
}