• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet.destroy()

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Could you confirm the answer to the question
Question:Assuming that the Servlet Container has just called the destroy() method of a servlet instance, which of the following statements are correct?
Choices:
1.The service method of this instance has been called once.
2.The init method of this instance has been called once.
3.The servlet container method can bring this instance by calling its init method.
4.There are no threads running over this instance�s service method.
5.All threads created by this servlet are done.
Answers: 2 & 4
I am convinced the answer but aren't the choices 4 and 5 similar in nature?
So 5 must also be an answer too.
Thanks
Ravi
 
Sathya Sankar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...but aren't the choices 4 and 5 similar in nature?

No.
4. There are no threads running over this instance�s service method.

Threads here refer to request threads handed over by the container to the servlet. So this statement says that the servlet is not currently processing any requests.
5. All threads created by this servlet are done.

This statement refers to threads created within the service method of the servlet. Lets say the servlet creates n number of threads within it's service method, the service method could exit well before any of the threads spawned by it die. Thus the servlet itself might not be entertaining any request threads in it's service method but threads spawned by it servicing a request earlier could still be running.
Hope it helps... :roll:
Ciao,
GSS
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
If choice 2 is correct then shouldn't choice 1 be correct to?

This method is only called after the servlet's init() method has completed successfully.
 
Sathya Sankar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mathias,
If choice 2 is correct then shouldn't choice 1 be correct to?

NO.
This method is only called after the servlet's init() method has completed successfully

You are right about the sequence. But the service method will be called only if the servlet container receives a request for the servlet. Lets say the servlet container is started. The servlet container has the choice of preloading a servlet (creating an instance of the servlet so that a request for that servlet can be immediately processed). After creating the servlet it calls the init method immediately. Now lets say that a significant amount of time passes with no request received for that servlet. The servlet container can now "release" the servlet instance it contains. This is done by calling the destroy method and typically setting the servlet reference to null for garbage collection.
In this sequence, the service method is not called at all. Therefore if the destoy method is called there is no guarantee the service method would have been called. What is guaranteed is that an instance of the servlet has been created, the init method has been called for that instance and the servlet is currently idle with no threads running in it's service method.
Ciao,
GSS
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
That help alot
// Mathias
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sathya for the explanation.
I am still confused. If the threads spawned by the service method in the servlet have not finished then the service method is still being executed then in that case the container can't call the destroy method on the servlet.
-- Ravi
 
Sathya Sankar
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,
If the threads spawned by the service method in the servlet have not finished then the service method is still being executed...

No. If you refresh what you studied for SCJP, once threads are started using the thread.start method, they run parallelly along with the thread that started it. Therefore lets say the last statement in the service method spawns a new thread with a call like (new Thread(myRunnableObject)).start(). Now the new thread starts executing but the service method finishes executing.
Hope this helps...
Ciao,
GSS
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sathya, that helps!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic