7

I have read that Java threads are user-level threads and one of the differences between user level threads and kernel level threads is that kernel level threads are scheduled by the kernel(we cannot change it) where as for user level threads we can define our own scheduling algorithm.

So how do we schedule threads in Java? At any given time, when multiple threads are ready to be executed, the runtime system chooses the Runnable thread with the highest priority for execution. If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. What if I don't want RR? is there a way I can change it or am I missing something here?

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 3
    Trying to change such low level settings of the JVM is usually a very bad idea. What is the problem you are trying to solve with changing the execution order of threads? – ssindelar Jul 24 '13 at 07:11

5 Answers5

10

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.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
4

For the last 10 years or so JVM threads are system-level threads and not user-level ('green') threads. Even for user-level threads, you don't get to manage them (the JVM does).

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
1

The JVM Spec does not state how threads are supposed to be scheduled by an implementation. The Hotspot VM (and most likely almost every other implementation as well) use the OS scheduling mechanisms (as Uwe stated). See also What is the JVM Scheduling algorithm?.

A simple, yet most likely not very efficient way to influence scheduling of your application threads would be to have only n runnable threads for the OS to schedule (n being the number of threads you'd actually like to run in parallel). That could e.g. be your own implementation of ExecutorService, which makes all threads you don't want to be scheduled by the OS wait until you think they should run. Of course this way you don't have any influence on other VM threads, let alone other applications or the OS.

A lot more involved (and not plattform independent) would be to change the OS scheduler itself to something more tailored to the needs of a JVM. A quick google research found this abstract, and I guess there's more work done on that field.

Community
  • 1
  • 1
JohannesR
  • 1,646
  • 1
  • 12
  • 12
0

You could write your own thread scheduler, analogous to the Quartz job scheduler for batch jobs.

This would allow you to execute threads at various times of the day during the run of your application.

If all you want is to determine the order of your thread execution, execute the code from one master thread.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
-1

In Effective Java, 2nd Ed., Joshua Bloch devotes an item to the discussion of thread scheduling. He goes on at length about how trying to tweak thread scheduling usually only leads to solutions that are JVM implementation dependent, non-portable, and fragile.

If there's a particular scheduling problem that you have, then for new code you should not deal with low-level thread calls anyway. Java has higher level concurrency libraries that simplify many of these tasks. Rather than defining the solution to your problem with threads, you ought to be thinking of Executors and Tasks. There are also higher level facilities that simplify inter-thread communication, such as CountDownLatch.

Low level thread calls such as wait, notify, and notifyAll can be difficult to do properly.

scottb
  • 9,908
  • 3
  • 40
  • 56