Java – Understanding Tomcat/Spring Boot multithreading

Understanding Tomcat/Spring Boot multithreading… here is a solution to the problem.

Understanding Tomcat/Spring Boot multithreading

I created a simple web application based on spring-boot. It receives the request, performs some block operation (such as writing console numbers from 1 to 10_000) and returns a simple response.
When I started testing it with the JVisual VM, I noticed that the threads were working in a strange way.
In the beginning, at low loads, they work not at the same time, but in turns:

load test 1

If I try a high load application instead of Tomcat to create a new thread (which is fine as far as I know) and after a while the threads work at the same time :

load test 2

I’ve tested the application with Netling application. It’s just a simple request to localhost, nothing special.
Can you explain why it works this way? Or provide some links with descriptions.
Thanks in advance!

Solution

I have understood the source of the problem.
On the JVisualVM I see, all threads are blocking each other, but I don’t know why. Now it’s clear – I’ve used system.out.println() as the block operation, but threads can’t use it at the same time and the console is locked.
I’ve changed the console output to file output (using the thread name as the file name to prevent locking) and started the app again. Now it works as expected.

Stupid mistake, but I hope it works for someone 🙂

enter image description here

Related Problems and Solutions