• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Thread question

 
carl chan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP PAGE CODE:
<html><body>
<%@ page isThreadSafe="false" %>
<%! int j=0; %>
<%
for (int i=0; i<10; i++)
{
out.print("The value of j is " + j + "<br>");
j++;
Thread.currentThread().sleep(1000);
}
%>
</body></html>
if 3 users A,B,C open the above jsp file,the following is what user A,B,C see.
User A:
The value of j is 0
The value of j is 1
...
The value of j is 8
The value of j is 9
User B:
The value of j is 10
The value of j is 11
...
The value of j is 18
The value of j is 19
User C:
The value of j is 20
The value of j is 21
...
The value of j is 28
The value of j is 29
The question is why the jsp page is sharing the variable j?? Isn't the jsp container redirect the request to different servlet/jsp instance??
Please explain.
Thanks
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
note the page directive --> isThreadSafe="false"
This is the same as saying a Servlet does NOT implement the SingleThreadModel.
So... there's one instance of this JSP/Servlet, your application server has several "worker threads" that execute this ONE instance. So the instance variable 'j' is shared by all 3 users.
Check out the explanation of javax.servlet.SingleThreadModel to get an idea what it's about.
You NEED to understand the ramifications of using or not using SingleThreadModel (or isThreadSafe for a JSP) in a distributed environment for the SCWCD exam.
 
Mark Fletcher
Ranch Hand
Posts: 897
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jessica,
Surely using isThreadSafe="false" makes the servlet resulting from the page implement SingleThreadModel?
I base this on the the following link: Synchronization Issues.
The default for isThreadSafe is "true", its assumed that the developer has synchronized access to non thread safe variables.
If its "false", the servlet resulting from the JSP page implements SingleThreadModel.
For example the following JSP page:

The java class file generated by the container (in this case Jakarta Tomcat 4.1.24) looks like this:

Cheers,
Mark
[ April 06, 2003: Message edited by: Mark Fletcher ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops.... *blush* Mark's right -- I got things backwards. Nice catch Mark.
 
carl chan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys, I understand that the jsp is same as implemenets SingleThreadModel, but i don't understand why the three instance are sharing the same variable "j", I think the code <%! int j=0; %> already reset j=0 when ever a new instance was created. Isn't it true?
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by carl chan:
Sorry guys, I understand that the jsp is same as implemenets SingleThreadModel, but i don't understand why the three instance are sharing the same variable "j", I think the code <%! int j=0; %> already reset j=0 when ever a new instance was created. Isn't it true?

In case of SingleThreadModel, the container has two options. It can
a) either create only one instance of the servlet, queue the incomming requests, and then dispatch them one by one. When the servlet is servicing the first request other requests in the queue are blocked. The container can opt for this method if there are relatively few requests and blocking may not affect the response time.
b) or create multiple instances, one for each request. In this case, the requests are being serviced simultameously by different instances and are not blocked. The container can opt for this method if there are two many requests and blocking may affect the response time.
For example, the container may decide to queue 10 requests per instance. If 10 clients hit the servlet simultaneously, then the container may create only one instance, service the first request and block the other 9. However, if 50 clients hit simultaneously, then the container may create 5 servlet-instances with 10 requests in each servlet's queue.
Later, when the number of requests reduce to say, less then 10, the container can call destroy() on the other four instances and pull them out of service.
In your case, the container is creating only one instance. Hence, the first request-thread increments j from 0 to 9, then the second request from 10-19, etc. If it had created three instances, you would have got 0-9 for each thread.
Note that in either case, one-instance (#a) or multiple-instances (#b), "thread-safety" of the individual instance(s) is guaranteed. This means, in the above example, you will never see the output from two requests mixing the values as
UserA - 0,1,2,9,10,11,...
UserB - 3,4,5,12,13,14,...
UserC - 6,7,8,15,16,17,...
Hope that help
-j
 
carl chan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, IT helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic