java - Thread.yield() 仍然是跨平台雷区?

标签 java multithreading

我可以找到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/

相关文章:

java - 在 glassfish 中安装 awstats

linux - 如何在 GDB 中列出线程及其相应的函数?

c++ - Windows OS的I/O优先级

android - 处理程序线程 : Only one Looper may be created per thread

java - 如何找到类加载器尝试从哪个 JAR 加载特定类?

java - 有没有办法将参数传递给Runnable?

android - Ubuntu 10.04 64 位上的 Android 开发使用哪个 JDK?

java - hashmap put性能问题

python - Python中的多线程诅咒输出

c++ - MacOS X 中的 C++ 线程