国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Servlet和ThreadLocal的測試

2019-11-18 12:32:33
字體:
來源:轉載
供稿:網友

  public class TestThreadServlet extends HttpServlet {
  
    PRivate static ThreadLocal thread    = new ThreadLocal();
  
    private int         flag     = 0;
  
    public void doGet( HttpServletRequest request,
  
             HttpServletResponse response)
  
        throws ServletException, IOException {
  
      flag++;
  
      String str = "This is the first String." + new Object();
  
      if (thread.get() == null)
  
        thread.set(str);
  
      PrintWriter out = response.getWriter();
  
      out.println("<p>");
  
      out.println("<BR>flag : " + flag);
  
      out.println("<BR>sessionid : " + request.getSession().getId());
  
      out.println("<BR>servlet : " + this.toString());
  
      out.println("<BR>thread : " + thread.get());
  
      out.println("</p>");
  
    }
  
  }
  
  執行結果:
  
  Session 1:
  
  flag : 2
  sessionid : amGeaiVwKvL9
  servlet : test.other.TestThreadServlet@5f2db0
  thread : This is the first String.java.lang.Object@1ad6b4b
  
  Session 1:
  
  flag : 3
  sessionid : aR3GkcUQoXT-
  servlet : test.other.TestThreadServlet@5f2db0
  thread : This is the first String.java.lang.Object@6214f5
  
  由執行結果可以看出
  
  1 服務器對每個Servlet只創建一個實例。flag不停增加
  
  2 Session范圍內的ThreadLocal中對象唯一。不同的請求,Object的hashCode相同。
  
  3 不同的Session共享ThreadLocal,但內部對象不同
  
  另:后來有人提醒我,實際上在web.xml為同一個servlet配置不同的名字,將會是兩個不同的實例。也就是說,servlet的實例與配置有關。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 温州市| 巴彦淖尔市| 临夏市| 双流县| 蚌埠市| 土默特右旗| 聂荣县| 宁远县| 邯郸县| 枝江市| 阿克| 韶山市| 通江县| 交口县| 赞皇县| 景宁| 龙井市| 南溪县| 宁阳县| 克东县| 广德县| 沈丘县| 桐城市| 银川市| 通江县| 静海县| 合江县| 宽甸| 易门县| 桐庐县| 雷州市| 万全县| 利津县| 郸城县| 全南县| 卓尼县| 社旗县| 榆社县| 博罗县| 凤山市| 泰安市|