• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

very interesting question on SingleThreadModel

 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
please try this code.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class myThread extends HttpServlet implements SingleThreadModel
{
int count=0;
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
int myCount=++count;
try
{
Thread.sleep(6000);
}
catch(Exception e){}
pw.println("Number of times acceessed?=" +myCount);
}
}

Access it simultaneously through 4 different browser windows --10-12 times [
why is the increment in 'count' inconsistent?
I have tried this servlet both in weblogic and tomcat.
The point is that after accessing it through 4 or 5 browser windows(and refreshing these windows 10 --12 times ,the value of count may become 10 but in one of the other browser windows (refresh)its 1 )
you really have to try it to understand what i mean.
WHAT'S HAPPENING?

"Don't worry,be Happy"
Amer
[ May 15, 2003: Message edited by: Amer Khan ]
 
Arvind Chavar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amer,
What do you mean by inconsistent?.Are you expecting them to be no duplicates?,and you are getting some duplicates?.
If it is true that you are getting duplicates, then the implementaion of SingleThread model by serlet container should be that there are multiple instances of servlet are created.
Arvind
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvind
I know that the server is creating a pool of instances of this servlet,the point of concern is why the value of count is unpredictable.
please try the code.
thanks
Amer
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that there is only one instance of the servlet in the jvm and multiple threads are incrementing the variable and then printing it. SingleThreadModel guarantees that only one thread will access the instance at a time, but while the thread is sleeping another thread is allowed to use that instance. The number is incrementing sequentially and there are no duplicate numbers which would indicate a new instance.
Even if you removed the sleep method, I would still think that the jvm or os could preemptively multitask the instance and you would still see this type of behavior.
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quoted by john
"SingleThreadModel guarantees that only one thread will access the instance at a time, but while the thread is sleeping another thread is allowed to use that instance."
Hi John,
I am 200% sure that the server is creating a pool of instances .
have a look at this
I think it's got something to do with the state of a particular instance chosen from the pool ,to serve a particular request.

Thank God 'sun' has decided to get rid off SingleThreadModel interface in 2.4.
"Don't worry,be Happy"
Amer
[ May 16, 2003: Message edited by: Amer Khan ]
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The server may very well be creating a pool of instances but since this is an instance variable and you have no duplicate values displayed then the jvm is always providing the same instance for printing the line.
Think about it, instanceA is initialized valueA=0, instanceB is initialized valueB=0, browser1 calls for servlet, jvm provides instanceA, increments valueA, sleeps, browser2 calls for servlet, jvm provides instanceB, increments valueB, sleeps, instanceA wakes and prints valueA=1, instanceB wakes and prints valueB=1
...but it doesn't because the jvm provided the same instance to the second browser which incremented the only instance value and the browsers show values that increment based on the number of browsers acting on the one instance
That's why I keep saying that there is only one instance being used by the limited number of browsers we're using for this test. If you had some type of load testing software that could spawn multiple browser request then enough traffic may generate the additional instances and thereby create pages with duplicate values printed.
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear John ;
I think when the request for the servlet is recieved ,one of the instances from the pool is assigned to handle this request,if another request comes before the previous instance is freed ,then to handle this request a different instance from the pool is assigned.Sinc each instance in the pool is a different obj, these instances do not share their instance variables.Here value of myCount or count depends on which instance is assigned to handle the request.
"Don't worry,be Happy'
Amer
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it with your recommended four browsers and it wasn't enough traffic to cause the creation of additional instances so I wrote this little web page that will spawn 20 new windows. On my machine it created two instances to handle the request.

change o n l oad to onload, it wouldn't let me post the code.
[ May 15, 2003: Message edited by: John Hembree ]
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tweaked both the servlet code by changing the sleep to 1000 and bumped up the spawn count to 40. Then when I refreshed the html page, I had about 9 windows with the value of 1, so 9 instances, but then as the system progressed I had fewer matchings.
So multiple instances will be created and you can see the instance value as you would expect it when there is a big enough load on the JVM to warrant the creation of additional instances.
[ May 15, 2003: Message edited by: John Hembree ]
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john
In support of my theory.
here is an example:if an instance who's count has advanced to 7 is assigned,then the increment in myCount displayed will be 8,on the other hand if a brand new instance is assigned then the value displayed for myCount will be 1.
(to try this example after opening 5 browser windows keep refreshing,browser 1,then 2,3,1,5,3,4)

Amer
[ May 16, 2003: Message edited by: Amer Khan ]
 
John Hembree
hired gun
Ranch Hand
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amer Khan:
i hope u r seeing the same effect as me.

I believe I am, now hopefully I'll get a question like this on the exam. I'm ready for this one (I hope).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic