线程礼让指让当前正在执行的线程暂停,但不阻塞。将线程从运行状态转为就绪状态。让cpu重新调度,礼让不一定成功。
//线程礼让 //礼让不一定成功,看cpu心情 public class ThreadYield { public static void main(String[] args) { MyYield myield=new MyYield(); new Thread(myield,"a").start(); new Thread(myield,"b").start(); } } class MyYield implements Runnable{ @Override public void run(){ System.out.println(Thread.currentThread().getName()+"线程开始执行"); Thread.yield(); System.out.println(Thread.currentThread().getName()+"线程停止执行"); } }
标签:Thread,MyYield,线程,new,myield,礼让 From: https://www.cnblogs.com/zhang12345/p/17173645.html