Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Doubt in servlet

 
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
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit,
that's a nice explanation!
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
Author
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.
 
We don't have time to be charming! Quick, read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic