• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic