Spring 是否在休息控制器中为每个请求创建新线程?

Does Spring create new thread per request in rest controllers?

本来想学习非阻塞REST,但是先写了阻塞controller来做对比。令我惊讶的是 Spring 不会阻止传入请求。

简单的拦截服务:

@Service
public class BlockingService {

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

简单的 REST 控制器:

@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();
    }
}

我在想,当我使用 curl 从 4 个独立的终端发送 4 个请求时,我得到:

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...

但令我惊讶的是,我得到了:

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)

为什么第一个请求不会阻止处理请求?但是我没有创建新线程,也没有处理任何异步的东西?

我为什么要问这个?因为我想学习如何使用DeferredResult,但现在我觉得没有必要

它在阻塞一个线程的意义上是阻塞的:由servlet容器(Tomcat、Jetty等从线程池中取出的线程。 ,不是Spring)来处理您的请求。幸运的是,并发使用许多线程来处理请求,否则任何 Java Web 应用程序的性能都将是惊人的。

假设您有 500 个并发请求都需要 1 分钟才能完成,并且线程池有 300 个线程,那么将有 200 个请求排队,等待其中一个线程可用。

自 NIO 以来绝对没有!!! SpringWeb运行在web容器Tomcat或Netty中,创建线程是tomcat或Netty的工作,而不是springmvc或springwebflux。 如果您在 BIO 模型中使用 tomcat,那么每个请求肯定是新线程。 Netty当然是NIO了,tomcat支持NIO和APR,都是非阻塞的, Spring boot webmvc tomcat 是默认的NIO,nio以后不用担心创建多线程了。