20

I am really curious about how the JVM works with threads!

In my searches on the internet, I found some material about RTSJ, but I don't know if it's the right directions for my answers.

Can someone give me directions, material, articles or suggestions about the JVM scheduling algorithm?

I am also looking for information about the default configuration of Java threads in the scheduler, like how long does it take for every thread in case of time-slicing.

I appreciate any help, thank you!

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
mrcaramori
  • 2,503
  • 4
  • 29
  • 47

4 Answers4

23

There is no single Java Virtual Machine; JVM is a specification, and there are multiple implementations of it, including the OpenJDK version and the Sun version of it, among others. I don't know for certain, but I would guess that any reasonable JVM would simply use the underlying threading mechanism provided by the OS, which would imply POSIX Threads (pthreads) on UNIX (Mac OS X, Linux, etc.) and would imply WIN32 threads on Windows. Typically, those systems use a round-robin strategy by default.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • 1
    There is the Java Virtual Machine Specification (http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html) or "JVM spec". But when someone talks about "a JVM" or "the JVM", then they usually mean an implementaton of the spec and not the spec itself. – Joachim Sauer May 12 '10 at 09:36
  • @Sauer, yes, the term "the JVM" implies a single JVM, and while this makes sense in the context of a discussion about a particular JVM (e.g. the Sun HotSpot JVM), it does not make any sense without establishing which particular JVM is being discussed. – Michael Aaron Safyan May 12 '10 at 10:22
  • Personally I often find myself saying things like "the JVM will load the class ..." or "the JVM will make sure, that ..." which *definitely* refers to an implementation, although it's not relevant to the discussion which concrete implementation I'm talking about. On the other hand, when I'm talking specifically about the specification, then that should be noted "The JVM spec specifies, that ..." or something like that. – Joachim Sauer May 12 '10 at 10:29
7

It doesn't. The JVM uses operating system native threads, so the OS does the scheduling, not the JVM.

Chris Dolan
  • 8,905
  • 2
  • 35
  • 73
  • 3
    Some JVMs manage their own threads. The early Sun JVM did this. Some modern small embedded JVMs do this as well. – Will Hartung May 12 '10 at 03:58
  • @Will Hartung: true, http://en.wikipedia.org/wiki/Green_threads says the Hotspot JVM has used native threads since 1.2 days – Chris Dolan May 12 '10 at 04:04
  • no, the article says that the 1.1 JVM used them on Solaris, and I remember them being used by the Linux JVM when it was still called Blackdown, but I'm pretty sure that Hotspot (which became part of the JVM only in 1.3) never used green threads. – Michael Borgwardt May 12 '10 at 06:12
  • "Green threads" are a bit pointless on machines with more than one hardware thread. However there is a scheme (was used by JRockit, IIRC, not sure whether it still does), where it uses a number of native threads but they are not directly linked to Java threads. Might have been a particular optimisation for 32-bit systems (remember them?). – Tom Hawtin - tackline May 12 '10 at 12:14
2

I don't have commenting rights so writing is here... JVM invokes pthreads(generally used threading mechanism,other variants are there) for each corresponding request. But the scheduling here is done entirely by OS acting as host. But it is a preferred approach and it is possible to schedule these threads by JVM. For example in Jikes RVM there are options to override this approach of OS decision. For example, in it threads are referred as RVMThread and they can be scheduled/manipulated using org.jikesrvm.schedular package classes. For more reference

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27
2

A while ago I wrote some articles on thread scheduling from the point of view of Java. However, on mainstream platforms, threading behaviour essentially depends on underlying OS threading.

Have a look in particular at my page on what is Java thread priority, which explains how Java's priority levels map to underlying OS threading priorities, and how in practice this makes threads of different priorities behave on Linux vs Windows. A major difference discussed is that under Linux there's more of a relationship between thread priority and the proportion of CPU allocated to a thread, whereas under Windows this isn't directly the case (see the graphs).

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83