根據(jù)Tomcat官網(wǎng)中的介紹,對于一個瀏覽器請求,tomcat會指定一個處理線程,或是在線程池中選取空閑的,或者新建一個線程。
Each incoming request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). If still more simultaneous requests are received, they are stacked up inside the server socket created by the Connector, up to the configured maximum (the value of the acceptCountattribute). Any further simultaneous requests will receive "connection refused" errors, until resources are available to process them.
—— https://tomcat.apache.org/tomcat-7.0-doc/config/http.html
在Tomcat容器中,每個servlet是單例的。在SpringMVC中,Controller 默認(rèn)也是單例。 采用單例模式的最大好處,就是可以在高并發(fā)場景下極大地節(jié)省內(nèi)存資源,提高服務(wù)抗壓能力。
單例模式容易出現(xiàn)的問題是:在Controller中定義的實例變量,在多個請求并發(fā)時會出現(xiàn)競爭訪問,Controller中的實例變量不是線程安全的。
Controller不是線程安全的
正因為Controller默認(rèn)是單例,所以不是線程安全的。如果用SpringMVC 的 Controller時,盡量不在 Controller中使用實例變量,否則會出現(xiàn)線程不安全性的情況,導(dǎo)致數(shù)據(jù)邏輯混亂。
舉一個簡單的例子,在一個Controller中定義一個非靜態(tài)成員變量 num 。通過Controller成員方法來對 num 增加。
@Controller
publicclassTestController{
privateintnum=0;
@RequestMapping("/addNum")
publicvoidaddNum(){
System.out.println(++num);
}
}
在本地運行后:
-
首先訪問
http:// localhost:8080 / addNum
,得到的答案是1; -
再次訪問
http:// localhost:8080 / addNum
,得到的答案是 2。
兩次訪問得到的結(jié)果不同,num已經(jīng)被修改,并不是我們希望的結(jié)果,接口的冪等性被破壞。
從這個例子可以看出,所有的請求訪問同一個Controller實例,Controller的私有成員變量就是線程共用的。某個請求對應(yīng)的線程如果修改了這個變量,那么在別的請求中也可以讀到這個變量修改后的的值。
Controller并發(fā)安全的解決辦法
如果要保證Controller的線程安全,有以下解決辦法:
- 盡量不要在 Controller 中定義成員變量 ;
如果必須要定義一個非靜態(tài)成員變量,那么可以通過注解 @Scope(“prototype”)
,將Controller設(shè)置為多例模式。
@Controller
@Scope(value="prototype")
publicclassTestController{
privateintnum=0;
@RequestMapping("/addNum")
publicvoidaddNum(){
System.out.println(++num);
}
}
Scope屬性是用來聲明IOC容器中的對象(Bean )允許存在的限定場景,或者說是對象的存活空間。在對象進入相應(yīng)的使用場景之前,IOC容器會生成并裝配這些對象;當(dāng)該對象不再處于這些使用場景的限定時,容器通常會銷毀這些對象。
Controller也是一個Bean,默認(rèn)的 Scope 屬性為Singleton ,也就是單例模式。如果Bean的 Scope 屬性設(shè)置為 prototype 的話,容器在接受到該類型對象的請求時,每次都會重新生成一個新的對象給請求方。
- Controller 中使用 ThreadLocal 變量。每一個線程都有一個變量的副本。
publicclassTestController{
privateintnum=0;
privatefinalThreadLocaluniqueNum=
newThreadLocal(){
@OverrideprotectedIntegerinitialValue(){
returnnum;
}
};
@RequestMapping("/addNum")
publicvoidaddNum(){
intunum=uniqueNum.get();
uniqueNum.set(++unum);
System.out.println(uniqueNum.get());
}
}
以上代碼運行以后,每次請求 http:// localhost:8080 / addNum
, 得到的結(jié)果都是1。
更嚴(yán)格的做法是用AtomicInteger類型定義成員變量,對于成員變量的操作使用AtomicInteger的自增方法完成。
總的來說,還是盡量不要在 Controller 中定義成員變量為好。
-
Controller
+關(guān)注
關(guān)注
0文章
398瀏覽量
57138 -
singleton
+關(guān)注
關(guān)注
0文章
3瀏覽量
5275 -
線程安全
+關(guān)注
關(guān)注
0文章
13瀏覽量
2477 -
SpringMVC
+關(guān)注
關(guān)注
0文章
18瀏覽量
5780
原文標(biāo)題:如何保證 Controller 的并發(fā)安全
文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論