What Is the Jvm Thread Scheduling Algorithm

What is the JVM thread scheduling algorithm?

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.

Does OS thread scheduler gets override by JVM?

  1. who schedules the thread?

Operating System. Though, at application level, your JRE can schedule application-level threads based on the thread priority; still it'll get scheduled finally by the scheduler(scheduling block) of OS. User level threads are managed by a user level library, but they still require a kernel system call to operate.


  1. does the thread scheduler gets overridden anywhere?(like OS thread being overridden by JVM thread scheduler)

The Java runtime environment supports a very simple, deterministic scheduling algorithm called fixed-priority scheduling. The actual scheduler is unique in OS; and looks after scheduling of threads from overall perspective, not from Java/application level perspective.


  1. how do i change from preemptive scheduling to time slicing scheduling ? or vice versa?

You can't change the nature of scheduling of scheduler, unless you modify the OS kernel, which is lower-level stuff. Even in JRE, you can't change the thread scheduling at the application level.

Attribution: Thread Scheduling tutorial.

Can we change the JVM Thread Scheduler?

In general the JVM doesn't do any scheduling. That is the task of the OS. Linux for example has configurable scheduling options, and if you want to add a new scheduling strategy, you can change the kernel.

However, depending on why you want to do this, you can solve the problem another way such as using a custom Executor, or a Reactor style framework, or effectively disabling scheduling for a CPU and doing all the work in Java yourself. (Not a trivial topic, rarely very useful)

How to schedule Java Threads

You cannot change the scheduling algorithm as for the JVM this is outside the scope. The JVM uses the threading of user threads provided by the underlying OS.

So from the Java perspective you cannot change the scheduling algorithm. The scheduling is done automatically.

The only thing in Java you can do is set the priority of the thread. But how this affects the scheduling algorithm is not defined.

You can try to change the scheduling algorithm of the OS where your VM is running on. But this is highly dependend on the OS used.

If thread scheduler runs one thread at a time then how does java provide multi-threading?

Thread scheduler in java is the part of the JVM that decides which thread should run.

Close, but... Most JVMs that anybody uses for real work in the last couple of decades use "native threading". That means, that scheduling is the operating system's job. The JVM does not get involved.

There is no guarantee [of] which runnable thread will [next] be chosen to run by the thread scheduler.

True but..., The operating system implements a thread scheduling policy (It may be capable of implementing any of several policies, to be chosen by the system administrator). If you understand the policy, then you could, in principle, know which thread will be scheduled next.

But true, because the Java Language Specification does not say anything about thread scheduling policies, and if you're writing a "run anywhere" Java program, then you should not depend on any particular policy.

Only one thread at a time can run in a single process.

False, but... It was true, maybe thirty years ago. Virtually all computers, tablets, and cell phones these days have more than one processor. At any given instant, there can be one thread running on each processor.

The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

Yes. The thread scheduler on any given OS doesn't mainly do whatever, it implements a policy. On most computers, tablets, and cell phones the policy choices all will be preemptive. That means that a thread could be be de-scheduled at any point in time (e.g., half way through an i++; statement).

The alternative to preemptive multi-tasking is called cooperative multitasking, in which a thread can lose its turn on the processor only at certain, well defined yield points. That's what Thread.yield() is for, but I don't know where you are ever going to find Java running in a cooperative multitasking environment.

Process and threads scheduling in an operating system

Applications are developed via different languages and the different languages implement threading differently. There are basically 2 different implementations.

  1. Create separate Kernel Thread for each thread created in the application.
  2. Manage the application created thread within a main thread of application.

Note : Java's implementation could also vary JVM to JVM so it depends that which JVM and OS are used.

Coming to your next question. Scheduling will be on Threads not on Processes.

JVM Implementation of Thread Work Distribution and Multicore

For instance say Open JDK, I think I even do not know what parts I should take a look to read more about this.

You should start by looking at the parts of the source code that are coded in C++. A C / C++ IDE might help you with the codebase exploration.

Taking this to multicore, how is this mapping done for multicore? How threads are mapped to different cores to run simultaneously?

I'm pretty sure that the operating system takes care of that aspect, not the JVM.

... is it true to say that what JVM does through ThreadPool and ExecutorService is only creating threads and submitting tasks to them?

AFAIK, yes.

Controlling the java scheduling algorithm

He is talking through his hat. There is no Java thread scheduling algorithm. Threads are scheduled by the operating system, since at least 1999.

And even if there was, there is nowhere sensible that gives you the opportunity to rewrite it, short of implementing your own JVM.

Ask him how.



Related Topics



Leave a reply



Submit