Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Servlet life cycle? 1

Status
Not open for further replies.

TomBarns

Programmer
Feb 14, 2001
10
US
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?
 
Only a single instance of the servlet is created and then each new request results in a new thread that goes off to service(), doGet() or doPost().

Using synchronized guarantees that the method will not be executed concurrently by another thread. Any thread connecting to the servlet in synchronized mode has to obtain a monitor or 'lock' on the synchronized block/method. If another thread already has a monitor on the same synchronized block the first thread must wait.

It's a bit more efficient to just synchronize the lines of code you want to protect rather than the entire method, since that can cause a large overhead on the server.

So, rather than public synchronized void doGet()...

Code:
...
synchronized(this) {
  count++;
  out.println("Since creation this servlet has been accessed " + count + " times.");
}

Any extra copies of the object that are made will either be destroyed after the request has finished or during garbage collection.

Hope this helps,

Tiz
--
Tim <tim@planetedge.co.uk>
 
thanks for your reply.
i need just to make sure that i understood this:
if we have 5 requests come in in the same time,that means the servlet container will spawn 5 threads and each of them will run or execute a copie of that servlet class.(I'm taking here about without any synchronization applied)
is that true?
thanks again.
 
That is correct. Each request is another thread that calls the servlet via service(), doGet() or doPost(). --
Tim <tim@planetedge.co.uk>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top