Each Tomcat server runs on its own JVM and each JVM is a separate process in Operating System. Now I have deployed multiple application in Tomcat which has it own context and own Class Loaders. And if I run multiple Thread in each of this application, how Operating system handles this Thread switching and how entire JVM as process is switched with other process. How these JVM process and Java threads are related in terms of context switching. How it works in most of latest operating system.
Asked
Active
Viewed 340 times
1 Answers
1
In Linux threads are implemented mostly the same way as the processes. So scheduler doesn't care much about processes, it switches between threads instead. Read here more low level explanation.
Now JVM is a process which typically has a lot of threads. Each one of them is mapped one to one to some linux process. In that case scheduler will assign timeslaces(time for run for a particular thread) regardless what process(JVM in your case) owns this thread. This means if one JVM has ten times more threads in total than the other JVM, then the first JVM still has more time consuming CPU than the other one.
You can affect this behaviour in a different number of ways.
- You can change scheduler algorithm in your OS
- You can change priority of a particular thread. In that case it will get more time than other threads within the same JVM and threads from other JVM. You can assing priority both through java and linux terminal (nice command)
- You can bind a particular thread to some set of CPUs. Use taskset command. Each thread has its own PID which you can get with help of jstack utility(bundled with JDK)

Andrey Cheboksarov
- 649
- 1
- 5
- 9
-
Is there any book which explain these concepts in detail – user1894220 Jun 09 '17 at 17:53
-
@user1894220 "Linux Kernel Development" by Robert Love can be a good starting point. It explains a lot about scheduling. It has some kernel code details but you can omit it. "Understanding Linux Kernel" by Marco Cesatti explains everything in more detail but the book itself is a bit difficult to read – Andrey Cheboksarov Jun 09 '17 at 17:59