httpservlet, struts1, struts2线程安全,单实例,多实例几个关系
2012-09-14 18:18
267 查看
总结如下
线程安全条件:
1)常量始终是线程安全的,因为只存在读操作。
2)对构造器的访问(new 操作)是线程安全的,因为每次都新建一个实例,不会访问共享的资源。
3)最重要的是:局部变量是线程安全的。因为每执行一个方法,都会在独立的空间创建局部变量,它不是共享的资源。局部变量包括方法的参数变量。
线程不安全条件:
1)单实例多线程并且该实例有类变量
Runnable safe=new ThreadSafe();
Thread thread1=new Thread(safe,"thread1");
Thread thread2=new Thread(safe,"thread2");
线程不安全到安全转换:
1)加synchronized
2)? 把单实例多线程转换成多实例多线程
httpservlet:
它是单实例多线程,有可能线程不安全(当含有类变量),线程不安全转换安全方法
参考:http://tech.163.com/05/0523/14/1KEML1JS00091589_2.html
struts1:
它是单实例多线程,线程安全,因为没有类变量,执行通过execute
struts2:
它默认是单实例多线程,线程不安全,因为有类变量,线程不安全转换安全方法
就使用scope=prototype来指定是个原型模式,就变成多实例多线程。参考文章:
http://japi.iteye.com/blog/432617 http://wsqwsq000.iteye.com/blog/1120671 http://tech.163.com/05/0523/14/1KEML1JS00091589_2.html
线程安全条件:
1)常量始终是线程安全的,因为只存在读操作。
2)对构造器的访问(new 操作)是线程安全的,因为每次都新建一个实例,不会访问共享的资源。
3)最重要的是:局部变量是线程安全的。因为每执行一个方法,都会在独立的空间创建局部变量,它不是共享的资源。局部变量包括方法的参数变量。
线程不安全条件:
1)单实例多线程并且该实例有类变量
Runnable safe=new ThreadSafe();
Thread thread1=new Thread(safe,"thread1");
Thread thread2=new Thread(safe,"thread2");
线程不安全到安全转换:
1)加synchronized
2)? 把单实例多线程转换成多实例多线程
httpservlet:
它是单实例多线程,有可能线程不安全(当含有类变量),线程不安全转换安全方法
参考:http://tech.163.com/05/0523/14/1KEML1JS00091589_2.html
struts1:
它是单实例多线程,线程安全,因为没有类变量,执行通过execute
struts2:
它默认是单实例多线程,线程不安全,因为有类变量,线程不安全转换安全方法
就使用scope=prototype来指定是个原型模式,就变成多实例多线程。参考文章:
http://japi.iteye.com/blog/432617 http://wsqwsq000.iteye.com/blog/1120671 http://tech.163.com/05/0523/14/1KEML1JS00091589_2.html
相关文章推荐
- httpservlet, struts1, struts2线程安全,单实例,多实例几个关系
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- struts1和struts2线程安全对比
- struts2 整合spring 及 由spring治理struts的Action实例
- struts2.X心得9--struts2自定义拦截器以及文件上传等几个拦截器使用案例
- struts2.X心得9--struts2自定义拦截器以及文件上传等几个拦截器使用案例
- struts2-(2)---通过login示例解析几个struts.xml配置
- Struts2提交表单实例和struts主题
- 通过实例学习Struts2 - 从struts-blank开始 (1)
- [转]Struts1和Struts2 action的线程安全
- struts1 和struts2 线程安全
- Java ,单实例 多线程 ,web容器,servlet与struts1-2.x系列,线程安全的解决方案
- 通过实例学习Struts2 - 从struts-blank开始 (1)
- struts1和struts2线程安全
- Servlet、Struts1、Struts2线程安全
- 初谈struts1,struts2以及spring mvc线程安全
- struts2的action是线程安全的,struts1的action不是线程安全的真正原因
- struts1、struts2、spring mvc的action和HttpServlet、filter是否单例
- Struts2的action是否为线程安全?Struts1的区别?
- java struts2入门学习实例--使用struts进行验证