Here i come to seek your expert opinion once again. :-)
I am reading on Threads in the SCJP book by K&B.
They have done a great job explaining on Threads at one point they have explicitly mentioned,
. I understand this statement, but i'm just curious to know why? For exp in Operating System there are various Scheduling algorithms like FIFO, SJF, Priority, Round Robin which are used and their usage guarantees a fair allocation of resources to all. When i started learning Java, i read in some book sorry i cant recall in which but it said that the JVM is like a virtual OS or a connection between your java programs and the underlying Operating system of your machine.
When it comes to threads, very little is guaranteed
So if the Operating System of my machine can emply all these scheduling algorithms then why can't JVM?
Just curious about this woobly nature of Threads in JVM, perhaps you might help throw some light on this.
The more the underlying mechanisms are exposed, then the more tightly the code that is developed to run on that JVM becomes coupled to the implementation of that JVM, and so the less flexibility there is.
Ashish Dutt wrote:Matthew, like i said i'm just curious to know what are the scheduling algorithms that the JVM uses and why is it that the JVM is still not able to tackle this problem.
As I said, the JVM is able to tackle it. The JVM does, all the time. But you'd have to look at the implementation of the specific JVM you're interested in to find out how your works. (Personally, I haven't a clue!)
The original quote means that because we don't know what strategy is used, we can't make reliable predictions about the order in which threads are executed (without interfering with join statements, etc). It doesn't mean the JVM isn't behaving in a sensible and effective way.
Ashish Dutt wrote:For exp in Operating System there are various Scheduling algorithms like FIFO, SJF, Priority, Round Robin which are used and their usage guarantees a fair allocation of resources to all.
That's true, but "fair" does NOT mean predictable - at least not on an individual process basis.
If you start an OS process and then fork a dozen more from it, the OS offers no more guarantees about when each one of them will finish than the JVM does for Threads; all it guarantees is that, as far as it possibly can, each will be given a "fair crack of the whip".
Ashish Dutt wrote:Thanks Matthew and Mansukh for the insight.
Matthew, like i said i'm just curious to know what are the scheduling algorithms that the JVM uses and why is it that the JVM is still not able to tackle this problem.
I believe all modern JVMs now passes all scheduling duties to the Operating System -- meaning it is the operating system that schedules the threads. I don't know of any JVMs that does thread scheduling at user level anymore.