It's not a secret anymore!*
The moose likes Beginning Java and the fly likes The wobbly nature of Java Threads or should it be the wobbly nature of JVM in handling threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The wobbly nature of Java Threads or should it be the wobbly nature of JVM in handling threads?" Watch "The wobbly nature of Java Threads or should it be the wobbly nature of JVM in handling threads?" New topic
Author

The wobbly nature of Java Threads or should it be the wobbly nature of JVM in handling threads?

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Dear all,
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,
When it comes to threads, very little is guaranteed
. 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.
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.


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

It can apply these sort of strategies. The JVM you are using probably uses one or more of them. But the point is that you, as the programmer, shouldn't need to know what strategy is being used. This is a good thing, because it means that your code will work fine on a different JVM that uses different strategies - e.g. maybe an update switches to a strategy that has been shown to be more effective.

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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Read this article. JVM is an implementation of the JLS. You can create your own JVM and design it to use any algorithm that you want it to use or re use the underlying OS's algorithms.


~ Mansukh
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8043
    
  22

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".

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The wobbly nature of Java Threads or should it be the wobbly nature of JVM in handling threads?