线程礼让

suaxi
2020-11-25 / 0 评论 / 154 阅读 / 正在检测是否收录...

线程礼让

Thread.yield();

实例:

package com.thread.state;

/**
 * @Author suaxi
 * @Date 2020/11/25 21:31
 */
public class TestYield {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"A").start();
        new Thread(myYield,"B").start();
    }
}

class MyYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始执行");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"线程执行结束");
    }
}

注:礼让的结果最终由CPU调度决定
线程礼让01.png

线程礼让02.png

0

评论 (0)

取消