首页 > 技术文章 > springmvc中的controller是不是多线程的问题,以及会不会出现拥塞

chen-hui 2019-03-03 21:09 原文

https://bbs.csdn.net/topics/392049342?page=1

https://blog.csdn.net/wjs19930820/article/details/79848531

https://bbs.csdn.net/topics/392325919?page=1

 

springmvc默认是单例的,每一个请求进入,都会启动一个线程,会存在线程安全问题,即最好不要在controller,service层使用全局变量,如果存在对全局变量的修改,会出现线程安全问题。

https://www.cnblogs.com/xjx199403/p/10691659.html

 

测试验证的代码:

@RestController
public class MyTest {
public int count=0;

@RequestMapping("mytest")
public String mytest() throws InterruptedException {
int temp = count;
System.out.println(temp);
int j=0;
while (j<2000000000){
j++;
}
count++;
return "jjj";
}

}

 

连续请求,观察输出的值即可发现问题。或者用测试工具jmeter测试,则去掉里面的while循环。

 

推荐阅读