Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic