Servlet究竟是單例仍是多例你瞭解嗎?

作爲一個Java Web開發者,你必定了解和學習過Servlet。或許還曾在面試中被問到過Servelt是單例仍是多例這個問題。
web


遇到這個問題,你是否曾深刻了解過,仍是百度或者Google了一下,獲得答案就OK了呢?面試


咱們今天從Servlet規範及Tomcat源碼實現的角度,分析下這個問題。tomcat


在Servlet規範中,對於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 SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.app


上面規範提到,分佈式

  • 若是一個Servlet沒有被部署在分佈式的環境中,通常web.xml中聲明的一個Servlet只對應一個實例ide


  • 而若是一個Servlet實現了SingleThreadModel接口,就會被初始化多個實例。實例有多少呢,這裏沒細說。學習


下面再從Tomcat的源碼中找尋下具體的參考實現是什麼樣子的。如下代碼來源於Tomcat的StandardWrapper類。我把其中不太相關的部分作了刪除。this



public Servlet allocate() throws ServletException {spa

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); //若是實現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;

}


那一個實現了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;


因爲SingleThreadModel已經聲明爲廢棄,官方不建議使用。咱們這裏只是讓你們瞭解下。


總結下,一個Servlet究竟有幾個實例呢?受以下幾個緣由影響:

是否在分佈式環境中部署

是否實現SingleThreadModel,若是實現則最多會建立20個實例

在web.xml中聲明瞭幾回,即便同一個Servlet,若是聲明屢次,也會生成多個實例。



若是感受有用,請分享之,讓更多人看到。


歡迎關注,瞭解更多精彩。咱們的口號是Picture is cheap, show you the code. :)






本文分享自微信公衆號 - Tomcat那些事兒(tomcat0000)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索