aspose file tools*
The moose likes Servlets and the fly likes Life cycle of a servlet and threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Life cycle of a servlet and threads?" Watch "Life cycle of a servlet and threads?" New topic
Author

Life cycle of a servlet and threads?

Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
As we know that we send send a request for a servlet on the server,
the server will create just ONE INSTANCE of that servlet.
in other words just we have one object of that servlet,the life for that object
is based on the life of our server and it will be alive as long as our server is running.
let us say we need to send 50 requests almost in the same time to that servlet.
what I undersatnd the JVM that runs that object will creates 50 threads for those 50 requests.
my question is: Are those threads going to execute piece of code(a method within the servlet class)
(assuming that the piece of code(The method) is NOT SYNCHRONIZED) in the same time?
or the second thread for the second request will wait till the first thread of the first
request finish execution that piece of code.
are we going to have multiple copies of those memebers of that object(i do not think so)?
because we have just one object in the memory.
what i need to understand what is going to happen when we send more than request in the same time to
a servlet on the server?what is happening behind the scene?


Thanks for your help.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Here is what I understand...You are correct, each of those calls to the servlet will be a seperate thread, each with their own local methods. This is what we want. If any variables are defined within the class (not within the methods) and the variables get modified within the local methods, which are not synchronized, it could lead to inconsistencies.
I am sure someone else can give a better explanation.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
To explain the above phenomenon, we need to consider 2 architectures :
Servlet not implementing SingleThreadModel
In this case, there would be just ONE INSTANCE of servlet created.When multiple requests come in, each request is handled by a seperate thread, which is spawned by the servlet engine.Due to this, Servlets normally have Threading issues, depending on the scope of the declared variables,viz. static scope, instance scope and local scope.
If the scope is static or instance, then all the requests would share the copies, hence you would need to synchronize the access to these variables.However synchronization may lead to performance hits so you could consider the following alternatives.
The workaround would be to declare variables locally in these thread unsafe methods, viz. service(), doGet() and doPost().Since local variables are local to each thread, they counter the threading issues associated with the servlets.
However, sometimes you may come across lot of instance variables to be declared in the local scope.This might make your code clumsy.In that case, you may consider an alternative to declare an inner class with all those variables and instantiate this class in the local scope of the thread-unsafe methods

Servlet implementing SingleThreadModel
In this case, you have MORE THAN ONE INSTANCE of the servlet instantiated by the servlet engine.
So each request would be tackled by a separate instance, and most of us would feel that the threading problems are over.However, the fact is somewhat different.
If you look at the API, it says :

If a servlet implements this interface, the servlet will be thread safe. However, this interface does not prevent synchronization problems that result from servlets accessing shared resources such as static class variables or classes outside the scope of the servlet.

What does the above Para mean?
True, the instance variables declared at class level would be Thread-safe.But imagine you have a static variable declared,which are shared by all the instances.Are the access to the methods defined in these classes thread-safe.No, they are not.You would still need to synchronize access to these static variables, if you want correct results.
So to address the threading issues, you have the above 2 architectures to use.It actually depends on the type and size of the project, on which architecture would be beneficial.
The normal trade-offs are in the SingleThreadModel, performance decreases and scalabilty becomes a concern, if the number of users increases.In that case the first option would be better.However you may consider the second option, if you have estimated the number of users visiting your site, fairly correctly, since the first option, might provide speed hindrance.
Hope this helps,
Regards,



------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Thanks for the reply,it is very valuable.
what i understood that those threads are running in the same time and they run a copy of the servlet class.
i understand the issue with static and instance variable and how to get around that problem if the thread will miss use those and the solution to put them as local.
i want to make sure that all the thread run in the same time for those requests and they run or execute copies of the servlet class,is that right?
thanks.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hello Tom,
If your requirement is that all the requests should be handled with JUST one servlet instance, you have to see that:
1. Your servlet DOESNOT implement SingleThreadModel interface
2. Your servlet is initialized with just one set of initialization parameters.
The 2 point might be a bit tricky.For instance, if I have a servlet and load the properties file (refer the documentation of your web server!!) with one set of NAME=VALUE pair and you modify/add another set of NAME=VALUE pairs, then you would be having 2 servlet instances serving the requests, despite the fact that you are not implementing the SingleThreadModel interface
If the above 2 considerations are OK, then you would have only one servlet instance (copy, if this is what you are referring to!!) serving all the requests.

what i understood that those threads are running in the same time and they run a copy of the servlet class.
i want to make sure that all the thread run in the same time for those requests and they run or execute copies of the servlet class,is that right?

You can never be sure that all threads would run at same time.The control is with the servlet engine.You can only assume this.Actually, it depends on the WebServer architecture and the implementation algorithm which decides this.But when we code, we have to assume that one or more threads is executing our thread-unsafe methods.This is most likely to happen!!
Hope this helps.
Regards,
------------------
Sandeep Desai
vgdesai@vsnl.com

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Thanks alot.
Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Life cycle of a servlet and threads?