aspose file tools*
The moose likes Threads and Synchronization and the fly likes JVM & Java Threads Scheduling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "JVM & Java Threads Scheduling " Watch "JVM & Java Threads Scheduling " New topic
Author

JVM & Java Threads Scheduling

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi everyone,

I have read a article that when it comes to multi processor systems , we can't get the advantage of running multiple threads simultaneously since every threads are in a single JVM process. Indeed when I go further, I got to know that some JVM implementations are built to map Java thread to OS level native threads.

My question is this, by default I used sun's JVM 1.6 implementations which is widely spread among Java newbies.So
Q1.How does this modern JVM handle thread scheduling ?
Q2.Does it handle JVM itself or map Java threads to native threads ?
Q3.So what will happens to platform independence when operating system handles thread scheduling?

Please somebody give me clear answer.

Thanks & Regards,
Nuwan Arambage




Thinker
Nuwan Arambage
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

There is no default behaviour. It depends on the platform the JVM runs on. Java will usually use native threads, but on some operating systems it uses so called "green threads", which the JVM handles itself and is executed by a single native thread.

You shouldn't have to worry about this. It is all handled by the JVM, and is invisible to the programmer. The only real difference I can think of is that on an implementation that uses green threads, there will be no performance gain from multi-threaded divide-and-conquer algorithms. However, the same lack of performance gain is true for implementations that use native threads, but run on a machine with a single core.
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi Stephan,
In nutshell, what do you mean by it depends on the platform ?. is it related to NT or Linux ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Stephan van Hulst wrote:There is no default behaviour. It depends on the platform the JVM runs on. Java will usually use native threads, but on some operating systems it uses so called "green threads", which the JVM handles itself and is executed by a single native thread.



To provide a bit more context, there hasn't been a "green threads" implementation in over 10 years. I believe all modern JVMs uses the native threading implementation; and I believe that all modern OSes do allow an application threads to use any (and even all) of the processors on a single machine.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Nuwan Arambage wrote:Hi Stephan,
In nutshell, what do you mean by it depends on the platform ?. is it related to NT or Linux ?


"depends on the platform" means exactly that. Threading behavior is handled by the operating system, hence, its behavior is dependent on the system hardware/OS/configuration that the JVM is running on.

Henry
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

As far as I know, the only OS where you still have choices is the Solaris OS. I believe Java 1.1 was all Java-controlled threads, but since Java 1.2 and above, it went to native threads. In Solaris you can control the behavior (or at least as recent as Java 5 you could).

So for Q1: Thread scheduling is almost exclusively handled by the OS
Q2: For most OS, threads are native but for Solaris there is some control over this
Q3: You don't have to worry about platform independence - the code will be independent. On Solaris you can tweak the performance using command line options when launching Java


Steve
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
thanks for everyone's contribution to make everything clear to the deepest..



Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Excuse me people,

I don't know whether I digest 'Platform Independence' buzz word correctly.You all have already told that threads are mostly depends on the operating system where java virtual machine runs.So java threading mechanism depends on underlying operating system configuration. In fact, I feel java's platform independence is deviated.

Could someone clarify what I was thinking seems real or hypothetical....

thanks...

Nuwan Arambage

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

The JVM itself and its internals (including the threading mechanism) are always dependent on the underlying operating system. However, the JVM hides all this stuff from the programmer. The language and the API provided by the JVM is platform independent. You can always use these the same way, regardless of the underlying mechanisms.

It's the same with Threads. The JVM may use platform dependent mechanisms to implement Threads, but these are invisible to you, and you don't have to worry about them. You always interact with Threads in the same manner, regardless of the machine you are working on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JVM & Java Threads Scheduling