Java学习总结15——实现SingleThreadModel,容器不为servlet创建新的实例的问题

出处:http://blog.csdn.net/newcman/article/details/7016419

在百度贴吧看到这样一个问题,于是自己给了答复,感觉这个问题还是蛮有意思的,所有记录下来。贴吧地址:

[html]  view plain copy
  1. http://zhidao.baidu.com/question/209371363.html  
问题:

听说实现SingleThreadModel,可以使容器对客户端的每个请求创建新的servlet实例,但是我实现了那个接口之后,还是不行阿。
比如,我给servlet增加属性int count = 0;在doGet()方法里System.out.println(count++);为什么不是每次访问都输出0,而是递增呢?
回答:

其实是有实例的,当一个servlet实现了SingleThreadModel接口之后就会有一个Stack来保存这些实例。 至于为什么每次访问都是递增的。是因为当请求的实例数大于当前实例数的时候才会重新去加载个实例,然后放到Stack中,很明显,楼主第一次请求完后该实例被回收到Stack中,第二次请求时发现Stack中有实例供调用,当然就用Stack中的实例了而不会去重新加载了。

可以查看org.apache.catalina.core.StandardWrapper类的allocate方法。

 

[java]  view plain copy
  1. public Servlet allocate() throws ServletException {  
  2.   
  3.        if (debug >= 1)  
  4.            log("Allocating an instance");  
  5.   
  6.        // If we are currently unloading this servlet, throw an exception  
  7.        if (unloading)  
  8.            throw new ServletException  
  9.              (sm.getString("standardWrapper.unloading", getName()));  
  10.   
  11.        // If not SingleThreadedModel, return the same instance every time  
  12.        if (!singleThreadModel) {  
  13.   
  14.            // Load and initialize our instance if necessary  
  15.            if (instance == null) {  
  16.                synchronized (this) {  
  17.                    if (instance == null) {  
  18.                        try {  
  19.                            instance = loadServlet();  
  20.                        } catch (ServletException e) {  
  21.                            throw e;  
  22.                        } catch (Throwable e) {  
  23.                            throw new ServletException  
  24.                                (sm.getString("standardWrapper.allocate"), e);  
  25.                        }  
  26.                    }  
  27.                }  
  28.            }  
  29.   
  30.            if (!singleThreadModel) {  
  31.                if (debug >= 2)  
  32.                    log("  Returning non-STM instance");  
  33.                countAllocated++;  
  34.                return (instance);  
  35.            }  
  36.   
  37.        }  
  38.   
  39.        synchronized (instancePool) {  
  40.   
  41.            while (countAllocated >= nInstances) {//请求的数量大于当前的实例数才会加载新的实例的  
  42.                // Allocate a new instance if possible, or else wait  
  43.                if (nInstances < maxInstances) {  
  44.                    try {  
  45.                        instancePool.push(loadServlet());//在这加载  
  46.                        nInstances++;  
  47.                    } catch (ServletException e) {  
  48.                        throw e;  
  49.                    } catch (Throwable e) {  
  50.                        throw new ServletException  
  51.                            (sm.getString("standardWrapper.allocate"), e);  
  52.                    }  
  53.                } else {  
  54.                    try {  
  55.                        instancePool.wait();  
  56.                    } catch (InterruptedException e) {  
  57.                        ;  
  58.                    }  
  59.                }  
  60.            }  
  61.            if (debug >= 2)  
  62.                log("  Returning allocated STM instance");  
  63.            countAllocated++;  
  64.            return (Servlet) instancePool.pop();  
  65.   
  66.        }  
  67.   
  68.    }  
当一个实例调用完之后会被回收到Stack中

 

[java]  view plain copy
  1. public void deallocate(Servlet servlet) throws ServletException {  
  2.   
  3.       // If not SingleThreadModel, no action is required  
  4.       if (!singleThreadModel) {  
  5.           countAllocated--;  
  6.           return;  
  7.       }  
  8.   
  9.       // Unlock and free this instance  
  10.       synchronized (instancePool) {  
  11.           countAllocated--;  
  12.           instancePool.push(servlet);  
  13.           instancePool.notify();  
  14.       }  
  15.   
  16.   }  

从以上的代码可以看出,SingleThreadModel接口使Servlet程序员产生虚假的安全感,认为它是线程安全的。实际上该接口并不能避免同步而产生的问题,如访问静态类变量或该servlet以外的类或变量,即使访问该servlet类自身的属性也得很小心(如那个问题所描述的问题)。所以Servlet 2.4中已经废弃了该接口,不建议写servlet的时候继承该接口。因而对于大多数的servlet来说都是单例的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值