Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 压路机会话安全违规_Java_Multithreading_Session_Concurrency_Atomicity - Fatal编程技术网

Java 压路机会话安全违规

Java 压路机会话安全违规,java,multithreading,session,concurrency,atomicity,Java,Multithreading,Session,Concurrency,Atomicity,具有以下代码: public static RollerSession getRollerSession(HttpServletRequest request) { RollerSession rollerSession = null; HttpSession session = request.getSession(false); if (session != null) { rollerSession = (RollerSession)session.

具有以下代码:

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。是这样吗?如果会话是从请求中获取的,那么会话可以由两个线程共享吗

是的,你是对的,那边还有一个可见性问题!根据和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);

--

我们就是为了这个!如果您能投票支持我的答案或选择作为问题的答案,我将不胜感激。