• Servlet线程安全性


    问题:使用以下的代码演示servlet的线程安全问题?

     1 public class MultiThreadQuestion extends HttpServlet {
     2     public int count = 0;
     3     public void doGet(HttpServletRequest request, HttpServletResponse response)
     4             throws ServletException, IOException {
     5         // 1. 验证服务器是以线程的方式处理用户请求的
     6         try {
     7             this.count++;
     8             Thread.sleep(5 * 1000);
     9         } catch (InterruptedException e) {
    10             e.printStackTrace();
    11         }
    12         response.getOutputStream().write(("线程名:"+Thread.currentThread().getName()+"count="+this.      count).getBytes());
    13     }
    14 }

    同时访问以上的代码发现三次同时出现3,这就是线程的并发访问的问题。

    解决方案就是:同步

    给访问共享资源的代码加锁。

     1 public class MultiThreadQuestion extends HttpServlet {
     2     public int count = 0;
     3     public void doGet(HttpServletRequest request, HttpServletResponse response)
     4             throws ServletException, IOException {
     5         // 1. 验证服务器是以线程的方式处理用户请求的
     6         synchronized (this) {
     7             try {
     8                 this.count++;
     9                 Thread.sleep(5 * 1000);
    10             } catch (InterruptedException e) {
    11                 e.printStackTrace();
    12             }
    13             response.getOutputStream().write(("线程名:"+Thread.currentThread().getName()+"count="+this.count).getBytes());
    14         }
    15     }
    16 }

    总结:

    1.如果直接使用同步关键字,那么会导致servlet的运行效率严重的降低。

    2.尽量避免使用servlet的成员变量。

    3.如果避免不了那么我们就需要使用线程安全的成员变量。

          ArrayList    Vector    HashSet  

          如果需要不是线程安全的集合可以使用Collections进行线程安全的转换

    4.ServletContext、HttpSession需要使用的时候一定要使用同步。

    5.在servlet中最好使用局部变量。

  • 相关阅读:
    bzoj 1217: [HNOI2003]消防局的设立
    [USACO09FEB]庙会班车Fair Shuttle
    bzoj 1052: [HAOI2007]覆盖问题
    bzoj 1974: [Sdoi2010]代码拍卖会
    bzoj 1835: [ZJOI2010]基站选址
    bzoj 1875: [SDOI2009]HH去散步
    bzoj 3295: [Cqoi2011]动态逆序对
    bzoj 2120: 数颜色
    P1032 字串变换
    简析拓扑排序
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3729297.html
Copyright © 2020-2023  润新知