Servlet多线程测试

  1. Servlet在运行时只有一个实例,但是service方法是多线程的
  2. 通过重写doGet方法进行测试
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		System.out.println(new Date()+":处理get请求------");
    		System.out.println("实例地址为:"+this+";hashCode:"+this.hashCode());
    		System.out.println("当前线程:"+Thread.currentThread());
    		
    	}
  3. 测试输出结果
    Sun Jan 14 19:31:28 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-4,5,main]
    Sun Jan 14 19:31:29 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-5,5,main]
    Sun Jan 14 19:31:29 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-6,5,main]
    Sun Jan 14 19:31:29 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-7,5,main]
    Sun Jan 14 19:31:30 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-9,5,main]
    Sun Jan 14 19:31:30 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-8,5,main]
    Sun Jan 14 19:31:30 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-10,5,main]
    Sun Jan 14 19:31:30 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-1,5,main]
    Sun Jan 14 19:31:30 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-2,5,main]
    Sun Jan 14 19:31:31 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-3,5,main]
    Sun Jan 14 19:31:31 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-4,5,main]
    Sun Jan 14 19:31:31 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-5,5,main]
    Sun Jan 14 19:31:31 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-6,5,main]
    Sun Jan 14 19:31:31 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-7,5,main]
    Sun Jan 14 19:31:31 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-9,5,main]
    Sun Jan 14 19:31:32 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-8,5,main]
    Sun Jan 14 19:31:32 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-10,5,main]
    Sun Jan 14 19:31:32 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-1,5,main]
    Sun Jan 14 19:31:32 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-2,5,main]
    Sun Jan 14 19:31:32 CST 2018:处理get请求------
    实例地址为:com.oracle.first.login@31f35411;hashCode:838030353
    当前线程:Thread[http-nio-8080-exec-3,5,main]
  4. 由以上测试可知,Servlet实例只有一个,service方法是多线程的,当前线程被占用时,容器会调用新的线程进行处理请求
  5. tomcat8采用循环分配线程的算法,进行调用service方法

你可能感兴趣的:(javaEE)