• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

threadSafety.jsp SCWCD study kit example

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
i have a question about a thread safety. I dont understand the output of running this example. This is the example p.196 of manning scwcd study kit. I have slightly modified the example
<html><body>
<%@ page isThreadSafe="false" %>
<%! int j=0; %>
<%
for (int i=0; i<4; i++)
{
out.print("The value of j is " + j + "<br>");
j++;
Thread.currentThread().sleep(1000);
}
%>
</body></html>
Now when isthreadsafe is set to false this means it uses the singlethread model, therefore forcing the container to use a different instance for each thread. Now...i open this example with 2 browser windows, and run the first, then immediately i switch to the second and run it.
I see the first one start off saying value of j is 0, 1 , 2 ,3 (as expected). Thats fine, then i see the second (running concurrently) saying 4, 5, 6, 7 (not as expected). Now i would have thought the the second would be a new instance, thefore j would have a new value starting from zero ??? this is my problem
Could anyone explain this to me???
PS Now if i didnt run both concurrently i would expect to see the same results and the container doesnt need to necessarily use 2 different instances with the singlethreadmodel, just not with the same thread at the same time.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I've extracted from the servlet 2.4 Specification

If false then the JSP container shall dispatch multiple
outstanding client requests, one at a time, in the order they
were received, to the page implementation for processing.
If true then the JSP container may choose to dispatch
multiple outstanding client requests to the page
simultaneously.


So we set the attribute value to be "false", then it will process the client request one at a time... I also got the result the same as you... But when I changed the value to be "true", it alternatively print out the result like "0,2,4,6" in the first page and "1,3,5,7" in the second page...
That's an interesting experiment for me too...
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also run this jsp through tomcat and got the same results.
It looks to me that the web container is running the jsp page through the same instance but one at a time. The numbers never repeat which show that it is thread safe (ie only one thread in _jspService at a time). The processing take quite a while indicating it is single threaded.
I have done a similar experiment on a servlet and got slightly different results. aTomcat created two instances of the servlet and ran all the requests through these 2 instances.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
please refer to this thread , especially the last two contribs.
The point to grasp here is that the servlet container doesn't have to create many instances of one servlet that implements SingleThreadModel: that's just a possiblity.
So, in your case, Tomcat has only created one instance, and prevent it from being accessed from many threads.
In this case, that has exactly the same effect as synchronize it.
HTH,
Cyril.
reply
    Bookmark Topic Watch Topic
  • New Topic