package ow.Java.code.Thread; public class RunnableYield { public static void main(String[] ARGS){ MyTr myTr = new MyTr(); Thread t1 = new Thread(myTr,"a"); Thread t2 = new Thread(myTr,"b"); Thread t3 = new Thread(myTr,"c"); t1.start(); t2.start(); t3.start(); } } class MyTr implements Runnable{ public void run() { for(int i = 0; i < 9; i++){ if(i == 3){ System.out.println(Thread.currentThread().getName()+""+i); Thread.currentThread().yield();//线程礼让 } } } }
输出结果
a3
c3
b3
总结
在某种程度上交替执行 就是不让一个家伙过多的连续运行