views:

94

answers:

3

Hi,

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.

+1  A: 

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.

aioobe
+4  A: 

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.

EJP
I did. she said "umm......"
Ceilingfish
@EJP: +1 and amen to that... Probably a tiny bit later than 1999 on Linux. I don't remember exactly when the JVM switched to native Posix thread library but it was indeed many moons ago.
NoozNooz42
So a bit of research on POSIX thread libraries would probably be the most useful resource if you wanted to find out more about the scheduling algorithm? I assume that this isn't true if you're code is running on Windows?
Ceilingfish
No. A bit of research into the operating ayatem's support for threads. This hasn't been in a library for ten years or so. And it is most unlikely to help you with your application' perormance. The best bets for that are faster hardware, more memory, less contention, bigger buffers, etc as usual.
EJP
+1  A: 

See the following article on Java Thread Scheduling:

http://www.roseindia.net/java/thread/ThreadPriorities.shtml

Just to be clear, this doesn't replace Java thread scheduler, but affects its behavior.

In Java , thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads . Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread of highest priority during ready-to-run state.

Priorities are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY) to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The default priority is 5(Thread.NORM_PRIORITY).

The methods that are used to set the priority of thread shown as:

Method Description

setPriority() This is method is used to set the priority of thread.
getPriority() This method is used to get the priority of thread.

DVK
Does it. How and where?
EJP
@EJP - if you were referring to v1. of the answer, I have changed since then
DVK
There is nothing there that lets you rewrite the Java scheduling algorithm, which is the topic of this thread. And roseindia is not exactly an authoritative source.
EJP