javaSE之多线程之礼让线程

礼让不一定成功,看CPU心情

package state;

//测试礼让线程

public class Yield {
    
    


    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()+"线程停止执行");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42794826/article/details/108957076