Concept of JVM thread and relation to OS thread

Viewed 390

Here is my fundamental understanding of the CPU and threads(naive!). The processor can run one thread per core.
System information on my laptop reads as show below
Processor Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz, 2112 Mhz, 4 Core(s), 8 Logical Processor(s) **Can run 8 threads in parallel **
In order to validate my understanding i create a Spring Boot(embedded tomcat) to handle each request

@GetMapping("/ping")
public String ping(@RequestParam String id) throws InterruptedException {
System.out.println(MessageFormat.format("The request id is {0}", id));
int i = Integer.parseInt(id);
long now = System.currentTimeMillis();
long period = 5000L;
long later = System.currentTimeMillis();
if (i % 2 == 1) {
  while (later - now <= period) {
    later = System.currentTimeMillis();
  }
}
return PING_SUCCESSFUL;

}

I also set the max threads on tomcat to the following

server.tomcat.max-threads=200

I now use Apache JMeter to trigger 200 requests in a duration of 1 seconds

My expectation is my system is constrained to run only 8 threads and hence the total run time of the requests should be at least (200 / 8)*5 = 125 seconds
However even the 125 seconds is not realistic as there are other applications running on my system like Browser, JMeter, IntelliJ which should account for some threads themselves.

I observe a contrasting behavior - the total runtime observed is 5 seconds . How is this possible for the system to run more threads than the limit? ( I find some understanding amiss about the threads and how the underlying processors seem to paralelize the threads)

1 Answers

It is true that the system can run only 8 threads simultaneously, but the operating system schedules which of the eight are running at any given time and can both preempt and time slice processes to schedule other (waiting) processes for some portion of time. Java threads are isomorphic to native threads, so it's literally the operating system scheduling them (and if your computer worked like you thought, the network would stop working while your program ran).

Related