furry12
furry12

Reputation: 972

Spring boot web app acts like it's single-threaded

I'm testing a Spring Boot controller locally and I noticed some behaviour that surprised me. I wrote this examplary controller method :

@GetMapping()
public void test() throws InterruptedException {
    for(int i = 0; i < 5 ; i++) {
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + " " + i);
    }
}

And I called the endpoint twice from separate tabs almost at the same time. I was expecting to see log output overlapping from two threads (as I thought that each request would create a new execution thread). Meanwhile, the output I got was:

http-nio-8080-exec-1 0
http-nio-8080-exec-1 1
http-nio-8080-exec-1 2
http-nio-8080-exec-1 3
http-nio-8080-exec-1 4
http-nio-8080-exec-1 0
http-nio-8080-exec-1 1
http-nio-8080-exec-1 2
http-nio-8080-exec-1 3
http-nio-8080-exec-1 4

This looks like there's only one thread, and the second requested actually waited for the first one to finish. I thought the tomcat default max thread value was 200. Can someone please explain this behaviour to me? Why weren't there multiple threads created?

It's a blank project, created from Spring Initializer with only Spring Boot Starter Web as a dependency

Upvotes: 0

Views: 1016

Answers (1)

Shubham Dixit
Shubham Dixit

Reputation: 1

In Spring every request is executed in separate thread. For example,when 2 users want to login at the same time, JVM creates 2 threads: one thread for first user, another one for second user.

I tried your case and it works perfectly may be your requests are not that concurrent ,you can use curl

curl  http://localhost:8080/hello/Logan & curl http://localhost:8080/hello/Leon

Upvotes: 1

Related Questions