java 线程方法 ---- yiled()

 

class MyThread3 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 3; i++){
            // 当 i == 2 时, 线程让步, 当前线程也有可能再次抢占 cpu
            if (i == 1){
                Thread.yield();
            }
            System.out.println(Thread.currentThread().getName() + ": " + i);
        }
    }
}
public class Test3 {
    public static void main(String[] args) {
        new Thread(new MyThread3(), "线程A").start();
        for (int i = 0; i < 4; i++){
            System.out.println("主线程:" + i);
        }
    }
}

 

posted @ 2018-10-28 00:24  huanggy  阅读(472)  评论(0编辑  收藏  举报