RollerSession 具有以下代码:
public static RollerSession getRollerSession(HttpServletRequest request) {
RollerSession rollerSession = null;
HttpSession session = request.getSession(false);
if (session != null) {
rollerSession = (RollerSession)session.getAttribute(ROLLER_SESSION);
if (rollerSession == null) {
// HttpSession with no RollerSession?
// Must be a session that was de-serialized from a previous run.
rollerSession = new RollerSession();
session.setAttribute(ROLLER_SESSION, rollerSession);
}
....
我是并发问题的新手。这里似乎存在原子性冲突,两个不同的线程可能同时更新 setAttribute。是吗?如果会话是从请求中获取的,是否可以由两个线程共享?
是的,你是对的,更重要的是存在可见性问题!根据IBM的帖子和Java Ranch,get/set操作不是线程安全的。因此,如果你不希望应用中有任何争用条件,则应同步,但要小心将同步条件放在何处。
解释
执行请求线程的多个 Servlet 可能同时对同一会话对象具有活动访问权。容器必须确保以线程安全的方式执行表示会话属性的内部数据结构的操作。开发人员负责对属性对象本身进行线程安全访问。这将保护 HttpSession 对象中的属性集合免受并发访问,从而消除应用程序导致该集合损坏的机会。
这是安全的:
// guaranteed by the spec to be safe
request.getSession().setAttribute("foo", 1);
这是不安全的:
HttpSession session = request.getSession();
Integer n = (Integer) session.getAttribute("foo");
// not thread safe
// another thread might be have got stale value between get and set
session.setAttribute("foo", (n == null) ? 1 : n + 1);
——麦克道尔的回答