這篇文章主要介紹“Servlet是單例還是多例”,在日常操作中,相信很多人在Servlet是單例還是多例問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Servlet是單例還是多例”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、孟津網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為孟津等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
在Servlet規(guī)范中,對于Servlet單例與多例定義如下:
“Deployment Descriptor”, controls how the servlet container provides instances of the servlet.For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration. However, for a servlet implementing the SingleThreadModelinterface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.
上面規(guī)范提到,
如果一個Servlet沒有被部署在分布式的環(huán)境中,一般web.xml中聲明的一個Servlet只對應(yīng)一個實例。
而如果一個Servlet實現(xiàn)了SingleThreadModel接口,就會被初始化多個實例。實例有多少呢,這里沒細(xì)說。
下面再從Tomcat的源碼中找尋下具體的參考實現(xiàn)是什么樣子的。以下代碼來源于Tomcat的StandardWrapper類。我把其中不太相關(guān)的部分做了刪除。
public Servlet allocate() throws ServletException {
boolean newInstance = false;
if (!singleThreadModel) {
// Load and initialize our instance if necessary
if (instance == null) {
synchronized (this) {
if (instance == null) {
try {
instance = loadServlet();
} catch (ServletException e) {}}}}
if (singleThreadModel) {
if (newInstance) {
synchronized (instancePool) {
instancePool.push(instance); //如果實現(xiàn)STM接口,就放到一個棧里
nInstances++;
}}
} else {
if (!newInstance) {
countAllocated.incrementAndGet();
}
return (instance);
}
}
synchronized (instancePool) {
while (countAllocated.get() >= nInstances) {
// Allocate a new instance if possible, or else wait
if (nInstances < maxInstances) {
try {
instancePool.push(loadServlet());
nInstances++;
} catch (ServletException e) {}
} else {
try {
instancePool.wait();
} catch (InterruptedException e) {
// Ignore
}} }
countAllocated.incrementAndGet();
return instancePool.pop();
}}
/**
* Load and initialize an instance of this servlet, if there is not already
* at least one initialized instance. This can be used, for example, to
* load servlets that are marked in the deployment descriptor to be loaded
* at server startup time.
*/
public synchronized Servlet loadServlet() throws ServletException {
// Nothing to do if we already have an instance or an instance pool
if (!singleThreadModel&& (instance != null))
return instance; //注意此處,如果存在實例就直接返回
Servlet servlet;
try {
InstanceManager instanceManager = ((StandardContext)getParent()).getInstanceManager();
try {
servlet = (Servlet) instanceManager.newInstance(servletClass);
} catch (ClassCastException e) {
}
if (servlet instanceof SingleThreadModel) {
if (instancePool == null) {
instancePool = new Stack<>();
} //此處,使用Stack存放STM的Servlet
singleThreadModel = true;
}
initServlet(servlet);
} finally {
}
return servlet;
}
那一個實現(xiàn)了SingleThreadModel接口的Servlet,一般會初始化多少個實例呢?
StandardWrapper類中有兩個屬性,其中maxInstance初始為20。所以上面的問題就有了答案。
/**
* Does this servlet implement the SingleThreadModel interface?
*/
protected volatile boolean singleThreadModel = false;
/**
* Maximum number of STM instances.
*/
protected int maxInstances = 20;
到此,關(guān)于“Servlet是單例還是多例”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
本文題目:Servlet是單例還是多例
標(biāo)題URL:http://weahome.cn/article/ghsoii.html