我可以找到old references SO 的答案表明 Thread.yield() 的行为因平台而异。 This answer表示这是因为该行为实际上并未指定。
现代版本的 Java 中,Thread.yield()
行为是否会因平台而异?
编辑
我已经阅读了文档。我明白它的含义。。我问的是实际行为。
最佳答案
来自 Java 8 API (迄今为止最新的非测试版):
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.
Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.
It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions. It may also be useful when designing concurrency control constructs such as the ones in the java.util.concurrent.locks package.
其影响是:
- 是的,仍然依赖于平台
- 是的,启发式
关于java - Thread.yield() 仍然是跨平台雷区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978112/