解决方法:
- 不要定义成员变量。
- 万一必须要定义一个非静态成员变量时候,则通过注解@Scope(“prototype”),将其设置为多例模式。
@Controller
public class TestController {
private Integer typeValue = 0;
@RequestMapping("/setType/{type}")
public void setType(@PathVariable("type") Integer type) {
typeValue = type;
for(int i = 0; i < 10000000; i++){
test(type);
}
}
public void test(Integer type) {
System.out.println("当前:" + type + “,实际:” + typeValue);
}
}
操作流程
-
先访问localhost:8080/setType/0
-
再访问localhost:8080/setType/1
正常情况应该是当前:0,实际:0或者当前:!,实际:1
但在高并发下回出现当前:0,实际:1或者当前:1,实际:0
本文由 纸鸢's 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2021/03/11 16:18