一、默认线程
当我们开启一个SpringBoot的项目,我们并未设置任何和线程相关的操作,但是我们的程序还是可以执行多个请求。甚至说绝大多数的项目,我们不需要对线程这块做任何操作。
但是如果是单线程的话,它显然满足不了我们系统的需求,所有我们有必要了解一下,它默认的线程情况。
1-1、测试默认线程池
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController {
@GetMapping("/test") public String fun(){
System.out.println(Thread.currentThread().getName()); return "success"; }}
打印结果如下:我请求了14次
http-nio-8888-exec-1http-nio-8888-exec-2http-nio-8888-exec-3http-nio-8888-exec-4http-nio-8888-exec-5http-nio-8888-exec-8http-nio-8888-exec-7http-nio-8888-exec-6http-nio-8888-exec-9http-nio-8888-exec-10http-nio-8888-exec-1http-nio-8888-exec-2http-nio-8888-exec-3http-nio-8888-exec-4
可以看到它默认是有10个线程去执行我们的任务的。这个其实就是tomcat的默认线程我们可以在yml/properties里面进行配置
server: port: 8888 tomcat: min-spare-threads: 10 max-threads: 200
我们可以全局搜索一下这个min-spare-threads,这个json就是Sping的一些默认配置
可以看到里面配置了tomcat默认的线程数是10,最大线程数是200,而对于一般项目来说,这两个数字都已经够用了
1-2、定时任务默认线程
import org.springfr