image.png

    1. package com.pln.state;
    2. public class TestYield {
    3. public static void main(String[] args) {
    4. MyYield myYield = new MyYield();
    5. new Thread(myYield,"A").start();
    6. new Thread(myYield,"B").start();
    7. }
    8. }
    9. class MyYield implements Runnable{
    10. @Override
    11. public void run() {
    12. System.out.println(Thread.currentThread().getName()+"礼让开始");
    13. Thread.yield();//礼让方法
    14. System.out.println(Thread.currentThread().getName()+"礼让结束");
    15. }
    16. }