简体   繁体   中英

Correctness of thread priority algorithm

I read that the correctness of thread priority algorithm is not always guaranteed beacause it depends on the JVM. Why is so and how does it depend on the JVM?

Thanks in advance.

The thread priorities effect is platform specific. Playing with these priorities should be avoided as they can cause liveness problems and their effect is not obvious.

From the Java Concurency in practice:

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.

Conclusion:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM