论坛首页 入门技术论坛

我来证明dwr的application 是线程不安全的

浏览 3715 次
该帖已经被评为新手帖
作者 正文
   发表时间:2010-08-24   最后修改:2011-02-07
欢迎大家一起讨论扔砖


我就在网上简单搜了搜, 没找到关于其application的线程安全权威说法

自己看源码吧。。。


扫了一眼javax.servlet.ServletContext的层次结构如下

第二个MockServletContext是个静态内部类

其setAttribute方法居然这样,直接不考虑了

 
      public void setAttribute(String s, Object obj)
       {
       }


剩下org.directwebremoting.util.FakeServletContext了


其setAttribute方法如下:


    public void setAttribute(String name, Object value)
    {
        if(value != null)
            attributes.put(name, value);
        else
            attributes.remove(name);
    }




追加写入一个org.directwebremoting.util.FakeServletContext  其attributes 属性如下:


private final Map attributes;


attributes实例

public FakeServletContext(String resourceBasePath) {
		initParameters = new Properties();
		attributes = new HashMap();
		servletContextName = "FakeServletContext";
		this.resourceBasePath = resourceBasePath == null ? ""
				: resourceBasePath;
		String tempDir = System.getProperty("java.io.tmpdir");
		if (tempDir != null)
			attributes.put("javax.servlet.context.tempdir", new File(tempDir));
	}



显而易见了。。。。。

当然这是我所找到的实现,其他实现可能有所不同,至少Dwr的这个是不安全的,但是还有很多 实现 是线程安全的




  • 大小: 5.8 KB
   发表时间:2010-08-25  
servlet规范的实现有很多。你看的是哪一个?
0 请登录后投票
   发表时间:2010-08-25  
根据对这个东西作用的定位,它本来就不需要做到线程安全吧
0 请登录后投票
   发表时间:2010-08-25  
tel15618037751 写道
根据对这个东西作用的定位,它本来就不需要做到线程安全吧

同意...
0 请登录后投票
   发表时间:2010-08-25  
都 application 了肯定都是读操作 即使写也是定时器单线程写
0 请登录后投票
   发表时间:2010-08-25   最后修改:2010-08-25
application 一般都是init写,以后就读了。
0 请登录后投票
   发表时间:2010-08-25   最后修改:2010-08-25
ServletContext本来就是线程不安全的啊,LZ去看看那API吧,
0 请登录后投票
   发表时间:2011-02-07   最后修改:2011-02-07
zah5897 写道
ServletContext本来就是线程不安全的啊,LZ去看看那API吧,


不认同"zah5897"这个观点,我记得Lea在并发实践里说过sun对servlet规范没有强制要求是否安全,是各个实现不同而已。

以下是apache-tomcat-5.5.32 的  org.apache.jasper.servlet.JspCServletContext实现

public class JspCServletContext
    implements ServletContext
{

    public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL)
    {
        myAttributes = new Hashtable();
        myLogWriter = aLogWriter;
        myResourceBaseURL = aResourceBaseURL;
    }

    public Object getAttribute(String name)
    {
        return myAttributes.get(name);
    }
    
    ...
    
    protected Hashtable myAttributes;
}


这个可是线程安全的哦!,所以不认同"zah5897",规范都没说,何来你说的"ServletContext本来就是线程不安全的啊"???




再举个例子,也是apache-tomcat-5.5.32 的,这个是  org.apache.catalina.core.ApplicationContext

/**
 * Standard implementation of <code>ServletContext</code> that represents
 * a web application's execution environment.  An instance of this class is
 * associated with each instance of <code>StandardContext</code>.
 *
 * @author Craig R. McClanahan
 * @author Remy Maucherat
 * @version $Id: ApplicationContext.java 939525 2010-04-30 00:36:35Z kkolinko $
 */

public class ApplicationContext
    implements ServletContext {

...
    /**
     * The context attributes for this context.
     */
    protected HashMap attributes = new HashMap();

    /**
     * Return the value of the specified context attribute, if any;
     * otherwise return <code>null</code>.
     *
     * @param name Name of the context attribute to return
     */
    public Object getAttribute(String name) {

        synchronized (attributes) {
            return (attributes.get(name));
        }

    }

   /**
     * Bind the specified value with the specified context attribute name,
     * replacing any existing value for that name.
     *
     * @param name Attribute name to be bound
     * @param value New attribute value to be bound
     */
    public void setAttribute(String name, Object value) {
        ...
        // Add or replace the specified attribute
        synchronized (attributes) {
            // Check for read only attribute
            if (readOnlyAttributes.containsKey(name))
                return;
            oldValue = attributes.get(name);
            if (oldValue != null)
                replaced = true;
            attributes.put(name, value);
        }
        ...

    }
...
}



这个也是线程安全的!  ,建议"zah5897"多多看看源代码吧!


0 请登录后投票
   发表时间:2011-03-20  
ldbjakyo 写道
zah5897 写道
ServletContext本来就是线程不安全的啊,LZ去看看那API吧,


不认同"zah5897"这个观点,我记得Lea在并发实践里说过sun对servlet规范没有强制要求是否安全,是各个实现不同而已。

以下是apache-tomcat-5.5.32 的  org.apache.jasper.servlet.JspCServletContext实现

public class JspCServletContext
    implements ServletContext
{

    public JspCServletContext(PrintWriter aLogWriter, URL aResourceBaseURL)
    {
        myAttributes = new Hashtable();
        myLogWriter = aLogWriter;
        myResourceBaseURL = aResourceBaseURL;
    }

    public Object getAttribute(String name)
    {
        return myAttributes.get(name);
    }
    
    ...
    
    protected Hashtable myAttributes;
}


这个可是线程安全的哦!,所以不认同"zah5897",规范都没说,何来你说的"ServletContext本来就是线程不安全的啊"???




再举个例子,也是apache-tomcat-5.5.32 的,这个是  org.apache.catalina.core.ApplicationContext

/**
 * Standard implementation of <code>ServletContext</code> that represents
 * a web application's execution environment.  An instance of this class is
 * associated with each instance of <code>StandardContext</code>.
 *
 * @author Craig R. McClanahan
 * @author Remy Maucherat
 * @version $Id: ApplicationContext.java 939525 2010-04-30 00:36:35Z kkolinko $
 */

public class ApplicationContext
    implements ServletContext {

...
    /**
     * The context attributes for this context.
     */
    protected HashMap attributes = new HashMap();

    /**
     * Return the value of the specified context attribute, if any;
     * otherwise return <code>null</code>.
     *
     * @param name Name of the context attribute to return
     */
    public Object getAttribute(String name) {

        synchronized (attributes) {
            return (attributes.get(name));
        }

    }

   /**
     * Bind the specified value with the specified context attribute name,
     * replacing any existing value for that name.
     *
     * @param name Attribute name to be bound
     * @param value New attribute value to be bound
     */
    public void setAttribute(String name, Object value) {
        ...
        // Add or replace the specified attribute
        synchronized (attributes) {
            // Check for read only attribute
            if (readOnlyAttributes.containsKey(name))
                return;
            oldValue = attributes.get(name);
            if (oldValue != null)
                replaced = true;
            attributes.put(name, value);
        }
        ...

    }
...
}



这个也是线程安全的!  ,建议"zah5897"多多看看源代码吧!



楼主的钻研精神很佩服,不过其实大部分的情况下我们写代码除了init的时候会朝application写之外,一般都是读操作,所以我倒是认为无论application的实现是否线程安全我都当它是个不安全的,可能我孤陋寡闻了,想了一下还真是想不出什么场景下必须要往application里写的
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics