线程礼让

image

package com.guo.demo03.yield;
//测试礼让线程
//礼让不一定成功,看cpu心情
public  class TsetYield {

    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()+"-->start");
        Thread.yield();//礼让
        System.out.println(Thread.currentThread().getName()+"-->end");
    }
}


posted @ 2026-03-24 21:54  果子同志  阅读(10)  评论(0)    收藏  举报