gpt4 book ai didi

java - 线程优先级算法的正确性

转载 作者:行者123 更新时间:2023-12-01 12:07:28 24 4
gpt4 key购买 nike

我读到线程优先级算法的正确性并不总是得到保证,因为它取决于 JVM。为什么会这样?它是如何依赖于 JVM 的?

提前致谢。

最佳答案

线程优先级效果是特定于平台的。应避免使用这些优先级,因为它们可能会导致 active 问题,而且效果并不明显。

来自 Java 并发实践:

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.

关于java - 线程优先级算法的正确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27479385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com