Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in servlet

 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Assuming that the Servlet Container has just called the destroy() method of a servlet instance, which of the following statements are correct?

a.init() method of this instance has been called.
b.There are no container threads running over this instance's service method.
c.All threads created by this servlet are done.

I selected a and c.But the answer given is a and b; what is the difference between options b and c?

Thanks
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B says that when the destroy method is called, no thread is executing the service method. So you can think of it as service method is not serving any client request. This is true for destroy method which is always called after the last request is processed.

C is not true as the service method might start a thread which might not have completed when the destroy method is called. Take this code



Now when the service method is called, it will start a new thread and itself will end. Now when the container will call destroy method, it will not check whether the run method in MyThread class is complete or not. The destroy method might be called even when the run method in MyThread is still running...
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So what option b is trying to tell is that.destroy() will wait only for service() threads only and not for any other.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit,
that's a nice explanation!
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Ankit,
that's a nice explanation!


Thanks Ulf.

destroy() will wait only for service() threads only and not for any other


Yes! destroy will wait for any threads that are executing a service method (or in-fact doGet or doPost) then only it will execute...
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c.All threads created by this servlet are done.
On a side note, Servlets really shouldn't need to create their own threads, and it's a good idea to avoid it. Containers provide features of their own with inherent thread protection, and usually these are sufficient. Starting your own threads could lead to trouble.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic