多线程之线程礼让

下城礼让不一定成功,让CPU重新调度
 package com.yeyue.demo01;
 
 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() + "线程停止执行");
    }
 }

 

posted @ 2021-06-28 15:01  深夜暗月  阅读(43)  评论(0)    收藏  举报