根據(jù)Tomcat官網(wǎng)中的介紹,對(duì)于一個(gè)瀏覽器請(qǐng)求,tomcat會(huì)指定一個(gè)處理線程,或是在線程池中選取空閑的,或者新建一個(gè)線程。
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容器中,每個(gè)servlet是單例的。在SpringMVC中,Controller 默認(rèn)也是單例。 采用單例模式的最大好處,就是可以在高并發(fā)場(chǎng)景下極大地節(jié)省內(nèi)存資源,提高服務(wù)抗壓能力。
單例模式容易出現(xiàn)的問(wèn)題是:在Controller中定義的實(shí)例變量,在多個(gè)請(qǐng)求并發(fā)時(shí)會(huì)出現(xiàn)競(jìng)爭(zhēng)訪問(wèn),Controller中的實(shí)例變量不是線程安全的。
Controller不是線程安全的
正因?yàn)镃ontroller默認(rèn)是單例,所以不是線程安全的。如果用SpringMVC 的 Controller時(shí),盡量不在 Controller中使用實(shí)例變量,否則會(huì)出現(xiàn)線程不安全性的情況,導(dǎo)致數(shù)據(jù)邏輯混亂。
舉一個(gè)簡(jiǎn)單的例子,在一個(gè)Controller中定義一個(gè)非靜態(tài)成員變量 num 。通過(guò)Controller成員方法來(lái)對(duì) num 增加。
@Controller
publicclassTestController{
privateintnum=0;
@RequestMapping("/addNum")
publicvoidaddNum(){
System.out.println(++num);
}
}
在本地運(yùn)行后:
-
首先訪問(wèn)
http:// localhost:8080 / addNum
,得到的答案是1; -
再次訪問(wèn)
http:// localhost:8080 / addNum
,得到的答案是 2。
兩次訪問(wèn)得到的結(jié)果不同,num已經(jīng)被修改,并不是我們希望的結(jié)果,接口的冪等性被破壞。
從這個(gè)例子可以看出,所有的請(qǐng)求訪問(wèn)同一個(gè)Controller實(shí)例,Controller的私有成員變量就是線程共用的。某個(gè)請(qǐng)求對(duì)應(yīng)的線程如果修改了這個(gè)變量,那么在別的請(qǐng)求中也可以讀到這個(gè)變量修改后的的值。
Controller并發(fā)安全的解決辦法
如果要保證Controller的線程安全,有以下解決辦法:
- 盡量不要在 Controller 中定義成員變量 ;
如果必須要定義一個(gè)非靜態(tài)成員變量,那么可以通過(guò)注解 @Scope(“prototype”)
,將Controller設(shè)置為多例模式。
@Controller
@Scope(value="prototype")
publicclassTestController{
privateintnum=0;
@RequestMapping("/addNum")
publicvoidaddNum(){
System.out.println(++num);
}
}
Scope屬性是用來(lái)聲明IOC容器中的對(duì)象(Bean )允許存在的限定場(chǎng)景,或者說(shuō)是對(duì)象的存活空間。在對(duì)象進(jìn)入相應(yīng)的使用場(chǎng)景之前,IOC容器會(huì)生成并裝配這些對(duì)象;當(dāng)該對(duì)象不再處于這些使用場(chǎng)景的限定時(shí),容器通常會(huì)銷毀這些對(duì)象。
Controller也是一個(gè)Bean,默認(rèn)的 Scope 屬性為Singleton ,也就是單例模式。如果Bean的 Scope 屬性設(shè)置為 prototype 的話,容器在接受到該類型對(duì)象的請(qǐng)求時(shí),每次都會(huì)重新生成一個(gè)新的對(duì)象給請(qǐng)求方。
- Controller 中使用 ThreadLocal 變量。每一個(gè)線程都有一個(gè)變量的副本。
publicclassTestController{
privateintnum=0;
privatefinalThreadLocaluniqueNum=
newThreadLocal(){
@OverrideprotectedIntegerinitialValue(){
returnnum;
}
};
@RequestMapping("/addNum")
publicvoidaddNum(){
intunum=uniqueNum.get();
uniqueNum.set(++unum);
System.out.println(uniqueNum.get());
}
}
以上代碼運(yùn)行以后,每次請(qǐng)求 http:// localhost:8080 / addNum
, 得到的結(jié)果都是1。
更嚴(yán)格的做法是用AtomicInteger類型定義成員變量,對(duì)于成員變量的操作使用AtomicInteger的自增方法完成。
總的來(lái)說(shuō),還是盡量不要在 Controller 中定義成員變量為好。
原文標(biāo)題:如何保證 Controller 的并發(fā)安全
文章出處:【微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
Controller
+關(guān)注
關(guān)注
0文章
398瀏覽量
57557 -
singleton
+關(guān)注
關(guān)注
0文章
3瀏覽量
5324 -
線程安全
+關(guān)注
關(guān)注
0文章
13瀏覽量
2545 -
SpringMVC
+關(guān)注
關(guān)注
0文章
18瀏覽量
5955
原文標(biāo)題:如何保證 Controller 的并發(fā)安全
文章出處:【微信號(hào):AndroidPush,微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
評(píng)論