Action instance = null;
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);
if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}
return (instance);
}
// Create and return a new Action instance
if (log.isTraceEnabled()) {
log.trace(" Creating new Action instance");
}
try {
instance = (Action) RequestUtils.applicationInstance(className);
// :TODO: Maybe we should propagate this exception
// instead of returning null.
} catch (Exception e) {
log.error(
getInternal().getMessage("actionCreate", mapping.getPath()),
e);
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
getInternal().getMessage("actionCreate", mapping.getPath()));
return (null);
}
instance.setServlet(this.servlet);
actions.put(className, instance);
}
instance = (Action) actions.get(className);
此代码表情当带有同样URL的请求过来时,会拿同样的action。此时就会发生多线程资源共享的问题。所以如果action类的成员变量修改会传递到下一个使用action的线程。所以不建议在action中使用成员变量。另外只允许一个线程对action进行操作!
本文探讨了在处理HTTP请求时,多个线程同时访问同一个Action实例可能导致的数据共享问题。通过实例展示了如何在Java环境中实现线程安全的Action管理,确保每个请求得到独立且正确的响应。
7611

被折叠的 条评论
为什么被折叠?



