为什么struts2的action是线程安全的,struts1的action不是线程安全的? 
先对struts1和struts2的原理做一个简单的讲解

对于struts1 ,当第一次**.do的请求过来时,在内存中的actionmapping中找到相对应的action,然后new出这个action放在缓存中,当第二次一样的请求过来时,还是找的这个action,所以对于struts1来说,action是单实例的 ,只有一个,如果在action中定义变量,就要非常小心了,因为并发问题,可能带来灾难性的后果,也不是不可以,我们可以加锁达到同步,只是在性能上就要折衷了。

另外说几句 ,当struts交由spring管理的时候 ,spring的bean配置默认是单例的 , 
如果action是有状态的 ,必须显示的配置为prototype

  1. <bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
  2. <property name="service" ref="userService"></property>
  3. </bean>

下面是struts1.2的源码: 
当请求过来时,去找指定的action,如果有就直接取出来,如果没有就new一个新的action放到map中。

  1. /**
  2. * The set of Action instances that have been created and
  3. * initialized, keyed by the fully qualified Java class name of the
  4. * Action class.
  5. */
  6. protected HashMap actions = new HashMap();

processActionCreate这个方法里去一窥究竟吧: 
1、先获取类名 
2、根据类名去一个名为actions的map里查寻实例是否已经存在 
3、如果存在,则直接返回 
4、如果不存在,则创建一个新实例 
5、把创建好的action放到map里备用

  1. protected Action processActionCreate(HttpServletRequest request,
  2. HttpServletResponse response,
  3. ActionMapping mapping)
  4. throws IOException {
  5. // Acquire the Action instance we will be using (if there is one)
  6. String className = mapping.getType();//1、先获取类名
  7. ...
  8. Action instance = null;
  9. synchronized (actions) {
  10. // Return any existing Action instance of this class
  11. instance = (Action) actions.get(className);//2、根据类名去map里查寻实例是否已经存在
  12. if (instance != null) {
  13. return (instance); //3、如果存在,则直接返回
  14. }
  15. // Create and return a new Action instance
  16. //4、如果不存在,则创建一个新实例
  17. instance = (Action) RequestUtils.applicationInstance(className)
  18. instance.setServlet(this.servlet);
  19. actions.put(className, instance);//5、把创建好的action放到map里
  20. }
  21. ...
  22. return (instance);
  23. }

struts2 在struts1的基础上做了改进 ,对于struts2 ,每次请求过来都会new一个新的action , 所以说struts2的action是线程安全的 , 但同时也带来一个问题,每次都new一个action ,这样action的实例太多 , 在性能方面还是存在一定的缺陷的。

struts1是单例模式,而struts2是原型模式

struts2的action是线程安全的,struts1的action不是线程安全的真正原因的更多相关文章

  1. Struts2 源码分析——调结者(Dispatcher)之action请求

    章节简言 上一章笔者讲到关于struts2启动的时候加载对应的准备工作.如加载配置文件struts.xml之类的信息.而相应的这些操作都离不开Dispatcher类的帮助.如果读者只是认为Dispat ...

  2. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  3. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

  4. Struts2学习---namespace,file模块包含,默认action

    我们上一节已经将action基本的配置和使用讲了,接下来我们讲以下struts一些小知识点: namespac: 上一节学习action的时候我们访问我们jsp文件时候使用的: http://loca ...

  5. Struts2基础-3 -继承ActionSupport接口创建Action控制器+javaBean接收请求参数+ 默认Action配置处理请求错误 + 使用ActionContext访问ServletAPI

    1.目录结构及导入的jar包 2.web.xml 配置 <?xml version="1.0" encoding="UTF-8"?> <web ...

  6. struts2学习笔记之十四:使用注解配置Action(不是和spring集成使用)

    Struts2支持使用注解配置Action,减少配置文件的配置 Struts2如果要支持注解配置Action,需要插件的支持,导入插件struts2-convention-plugin-2.1.8.1 ...

  7. Netty In Action中文版 - 第十五章:选择正确的线程模型

    http://blog.csdn.net/abc_key/article/details/38419469 本章介绍 线程模型(thread-model) 事件循环(EventLoop) 并发(Con ...

  8. 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)

    ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.JDK 1.2的版本中就提供java.lang.ThreadLocal,使用这个工具类可以很简洁地编写出优美的多线程程序,Threa ...

  9. WPF异常“调用线程无法访问此对象,因为另一个线程拥有该对象 ”

    WPF中在对界面进行操作的时候,可能会遇到"调用线程无法访问此对象,因为另一个线程拥有该对象"异常,这是因为WPF中只有UI线程才能操作UI元素,非UI线程要访问UI时就会报异常了 ...

随机推荐

  1. centos6.5 yum安装MySQL5.6

    创建MySQL用户 #useradd mysql #passwd mysql #chmod u+w /etc/sudoers #vi /etc/sudoers mysql ALL=(ALL) ALL ...

  2. 前端预览图片和H5canvas压缩图片上传

    思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法得到base64字符串来实现压缩. 1.base64转二进制文件 /** * dataURL to blob, re ...

  3. mysql17---增量备份

    mysql增量备份: 全备份是: (增量备份一定要看日志的时间和位置节点) mysql数据库会以二进制的形式,把用户对mysql数据库的操作记录到文件中,不用使用定时器了.当用户希望恢复的时候,可以使 ...

  4. HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-3642 Jack knows that there is a great underground treasury in a ...

  5. 洛谷P4141 消失之物——背包

    题目:https://www.luogu.org/problemnew/show/P4141 竟然是容斥:不选 i 物品只需减去选了 i 物品的方案: 范围原来是2*10^3而不是2*103啊... ...

  6. bzoj1798 1

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 5866  Solved: 2079[Submit ...

  7. java笔记线程的生命周期图解

  8. “Live Desktop” privacy statement

    “Live Desktop” pays attention to your privacy protection. Sometimes we need some information to prov ...

  9. Excel:一列是源值随机加减某随机值,变为另一列的数值

    1) 产生x1与x2之间整数随机数 =RANDBETWEEN(x1,x2),x1和x2为随机数区间 如果需要小数,可以乘以小数获得,Eg: =RANDBETWEEN(-5,5)*0.01,表示 -0. ...

  10. Hibernate对集合属性的操作---基础学习

    1:Set集合属性操作 1).Hibernate3以后支持大部分重要的JDK集合接口映射,Set集合接口的配置:  >在xxx.hbm.xml文件中使用<set>标签 2).< ...