zoukankan      html  css  js  c++  java
  • Servlet中进行context属性的同步

    Servlet中进行context属性的同步;

    必须所有使用context的servlet都进行synchronized才可以实现同步;

    servlet:

    package com.stono.servlet.synchronize;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class TestServletA extends HttpServlet {
        private static final long serialVersionUID = 1L;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = req.getServletContext();
            Object attribute;
            synchronized (context) {// 为了防止在sleep过程中其他线程(servlet)修改context中的属性a;
                context.setAttribute("a", "a");
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                attribute = context.getAttribute("a");
            }
            PrintWriter writer = resp.getWriter();
            writer.println(attribute);
        }
    }
    package com.stono.servlet.synchronize;
    import java.io.IOException;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class TestServletB extends HttpServlet {
        private static final long serialVersionUID = 1L;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext context = req.getServletContext();
            synchronized (context) {// 必须这个类也进行synchronized才可以实现同步;这种情况下,这个servlet也会等带3秒的;
                context.setAttribute("a", "change to b");
            }
        }
    }

     servlet实现SingleThreadModel是不行的,该接口已经被废弃,而且只是对某一个servlet实现单线程处理;

    不能保证线程之间的安全性;

  • 相关阅读:
    C# 并行线程调用
    Oracle定时备份
    读取Excel里面的内容转为DataTable
    c# 将json数据转为键值对
    Py基础+中级
    深入理解DIP、IoC、DI以及IoC容器(转载)
    错误页面的配置
    JavaScript重载
    关于为空必填js判断
    MyEclipse CI 2018.8.0正式发布(附下载)
  • 原文地址:https://www.cnblogs.com/stono/p/4846662.html
Copyright © 2011-2022 走看看