线程礼让
1 package 多线程练习; 2 3 /* 4 线程礼让不一定每次都成功 看CPU调度 5 */ 6 public class ThreadYieldTest { 7 public static void main(String[] args) { 8 Runnable rb1 = ()->{ 9 System.out.println(Thread.currentThread().getName()+" 开始执行"); 10 Thread.yield(); // 线程礼让 11 System.out.println(Thread.currentThread().getName()+" 执行结束"); 12 }; 13 14 new Thread(rb1,"线程A").start(); 15 new Thread(rb1,"线程B").start(); 16 } 17 }
输出结果