yield方法是Thread类的静态方法,也是一个native方法。
调用方式:Thread.yield();
java源码中对这个方法的注释是:
A hint to the scheduler that the current thread is willing to yield * its current use of a processor. The scheduler is free to ignore this * hint.
简单来说,就是礼让下。
源码注释也说了:
It is rarely appropriate to use this method.
也就是说,这个方法使用场景不多。
可以参考这篇文章:http://www.importnew.com/14958.html
通过这篇文章的这个例子可以对yield方法有点深入的理解
不过按照上面那篇文章运行下来,yield后我的电脑打印结果还是
2019-05-09 15:04:59.710 19347-19369/calc.superdy.myapplication I/System.out: I am Producer : Produced Item 0
2019-05-09 15:04:59.710 19347-19369/calc.superdy.myapplication I/System.out: I am Producer : Produced Item 1
2019-05-09 15:04:59.710 19347-19369/calc.superdy.myapplication I/System.out: I am Producer : Produced Item 2
2019-05-09 15:04:59.710 19347-19369/calc.superdy.myapplication I/System.out: I am Producer : Produced Item 3
2019-05-09 15:04:59.710 19347-19369/calc.superdy.myapplication I/System.out: I am Producer : Produced Item 4
2019-05-09 15:04:59.711 19347-19370/calc.superdy.myapplication I/System.out: I am Consumer : Consumed Item 0
2019-05-09 15:04:59.711 19347-19370/calc.superdy.myapplication I/System.out: I am Consumer : Consumed Item 1
2019-05-09 15:04:59.711 19347-19370/calc.superdy.myapplication I/System.out: I am Consumer : Consumed Item 2
2019-05-09 15:04:59.711 19347-19370/calc.superdy.myapplication I/System.out: I am Consumer : Consumed Item 3
2019-05-09 15:04:59.711 19347-19370/calc.superdy.myapplication I/System.out: I am Consumer : Consumed Item 4