多线程_线程礼让

线程礼让_yield


礼让成功

礼让不成功

package com.hao.Demo01;

//线程礼让
//礼让不一定成功,看CPU心情
public class TestYield {
    public static void main(String[] args) {
        MyYeild myYeild = new MyYeild();

        new Thread(myYeild,"a").start();
        new Thread(myYeild,"b").start();
    }

}
class MyYeild implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始");
        Thread.yield();//线程礼让
        System.out.println(Thread.currentThread().getName()+"线程停止");
    }
}

感谢秦老师
来源:https://www.bilibili.com/video/BV1V4411p7EF?p=13
posted @ 2021-02-10 18:41  多愚  阅读(37)  评论(0编辑  收藏  举报