我读到线程优先级算法的正确性并不总能得到保证,因为它依赖于JVM。为什么会这样,它如何依赖于JVM?
提前致谢。
答案 0 :(得分:1)
线程优先级效果是特定于平台的。应该避免使用这些优先级,因为它们可能导致活体问题并且其效果不明显。
来自Java Concurency的实践:
The thread priorities defined in the Thread API are merely scheduling hints.The Thread
API defines ten priority levels that the JVM can map to operating system scheduling
priorities as it sees fit. This mapping is platform‐specific, so two Java priorities
can map to the sameOS priority on one system and different OS priorities on another.
...
The thread priority mechanism is a blunt instrument, and it's not always obvious what
effect changing priorities will have; boosting a thread's priority might do nothing or
might always cause one thread to be scheduled in preference to the other, causing
starvation.
结论:
Avoid the temptation to use thread priorities, since they increase platform
dependence and can cause liveness problems. Most concurrent applications can use
the default priority for all threads.