码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet中进行context属性的同步

时间:2015-09-29 18:32:02      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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实现单线程处理;

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

Servlet中进行context属性的同步

标签:

原文地址:http://www.cnblogs.com/stono/p/4846662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!