Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring create new thread per request in rest controllers?

I wanted to learn non blocking REST, but first I wrote blocking controller for comparison. To my surprise Spring doesn't block incoming requests.

Simple blocking service:

@Service
public class BlockingService {

    public String blocking() {
        try {
            Thread.sleep(10000L);
        } catch (InterruptedException ign) {}
        return "Blocking response";
    }
}

Simple REST controller:

@Slf4j
@RestController
public class BlockingRestController {

    private final BlockingService blockingService;

    @Autowired
    public BlockingRestController(BlockingService blockingService) {
        this.blockingService = blockingService;
    }

    @GetMapping("blocking")
    public String blocking() {
        log.info("Starting blocking request processing...");
        return blockingService.blocking();
    }
}

And I was thinking that when I send 4 requests using curl from 4 separated terminals I get:

1. Starting blocking request processing... (console where spring is running)
2. (4 terminals waiting)
3. "Blocking response" (in 1st terminal)
4. Starting blocking request processing... (console where spring is running)
5. (3 terminals waiting)
6. "Blocking response" (in 2nd terminal)
And so on...

But to my surprise I got:

1. Starting blocking request processing... (console where spring is running)
2. Starting blocking request processing... (console where spring is running)
3. Starting blocking request processing... (console where spring is running)
4. Starting blocking request processing... (console where spring is running)
5. "Blocking response" (in 1st terminal)
6. "Blocking response" (in 2nd terminal)
7. "Blocking response" (in 3rd terminal)
8. "Blocking response" (in 4th terminal)

Why first request doesn't block processing requests? Yet I don't create new threads and I don't process anything asynchronous?

Why do I ask about it? Because I want to learn how to use DeferredResult, but now I don't see a need.

like image 937
user Avatar asked Dec 22 '17 08:12

user


People also ask

Does Spring use thread per request?

Before the introduction of Spring Webflux, Spring MVC used to have Tomcat as its default embedded server, which uses the Thread Per Request Model. Now, with Webflux, Netty has been preferred as a default, which uses the Event Loop Model.

Is Spring REST controller multithreaded?

It is multithreaded. Then you have some sort of configuration issue.

Is every request a new thread?

A: One thread per request. The Container doesn't care who makes the request—every incoming request means a new thread/stack.

Is spring boot REST API is multithreaded?

Multithreading is similar to multitasking, with the difference that it enables the running of multiple threads simultaneously, rather than processes. In Java, a thread refers to a sequence of programmed instructions which the operating system's scheduler manages.


Video Answer


2 Answers

It's blocking in the sense that it blocks one thread: the thread taken out of the pool of threads by the servlet container (Tomcat, Jetty, etc., not Spring) to handle your request. Fortunately, many threads are used concurrently to handle requests, otherwise the performance of any Java web application would be dramatic.

If you have, let's say, 500 concurrent requests all taking 1 minute to complete, and the pool of threads has 300 threads, then 200 requests will be queued, waiting for one of the threads to become available.

like image 103
JB Nizet Avatar answered Oct 14 '22 05:10

JB Nizet


Absolutely NO since NIO!!! Spring Web runs in a web container Tomcat or Netty, it is tomcat or Netty's work to create thread, not spring mvc or spring webflux. If you use tomcat in a BIO model, it is definitely new thread per request. Netty is of course NIO, tomcat supports NIO and APR, both are non-blocking, Spring boot webmvc tomcat is default NIO, no need to worry about creating to many threads since nio.

Tomcat 8 NIO,how it works?

like image 23
Miles Liu Avatar answered Oct 14 '22 07:10

Miles Liu