I was talking to an interesting interviewee today, who insisted that the best way to improve the performance of our Java application was to rewrite the thread scheduling algorithm. Given that we rely on the JVM thread scheduling algorithm I'm reasonably sure this isn't possible, but I was wondering if there's any techniques you can use to affect the scheduling algorithm. Or if there is even a compelling reason 开发者_如何学运维to do so.
PS The application in question doesn't have any serious performance problems. The interviewee was just a bit keen.
He is talking through his hat. There is no Java thread scheduling algorithm. Threads are scheduled by the operating system, since at least 1999.
And even if there was, there is nowhere sensible that gives you the opportunity to rewrite it, short of implementing your own JVM.
Ask him how.
I quite certain that the Java Language Specification doesn't specify concretely how threads (that is, the threads in the RUNNABLE state) are scheduled. It probably requires some kind of fairness, but the details are most likely up to the JVM implementor to decide (which means that you don't have any more control over it than the JVM in question provides you with).
For efficiency reasons, most of them probably defer the task to the OS straight away.
I'll see if I can find some references.
You can "tweak" the scheduling of course, by for instance setting the priorities (from the docs on Thread: Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.), or by synchronizing the threads using monitors.
精彩评论