This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes single instance of servlet (interview question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "single instance of servlet (interview question)" Watch "single instance of servlet (interview question)" New topic
Author

single instance of servlet (interview question)

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 382
    
    2
Hi ,

I was asked how only a single instance of a servlet could be created. I told implementing the SingleThreadModel interview. I assume its not depricated. The interviewer asked that even if you implement SingleThreadModel interface the webcontainer would make multiple instances of the Servlet.

I would appreciate if any one could explain me how this can be achieved.

Thank you.


SCEA part I,TOGAF Foundation
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you don't use SingleThreadModel (which is deprecated by the way), the container will create one instance of your servlet.
That one instance will handle all requests in a multi-threaded fashion.

With SingleThreadModel a new instance of the servlet will be created for each request (or for efficiency sake, the container may keep a pool of instances on hand and recylcle them).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Just for fun, try something like this:

... and you will see that AcmeServlet is being instantiated more than once.


There is no emoticon for what I am feeling!
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 382
    
    2
Hi Jeff,

Thank you for your answer. I have one more query, how to make a servlet ThreadSafe?

Thanks and Regards

Ayub.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Ayub ali khan:
Hi Jeff,

Thank you for your answer. I have one more query, how to make a servlet ThreadSafe?

Thanks and Regards

Ayub.


Try to use local variables where ever possible. Use synchroniztion for shared data.


Groovy
kamlesh tayade
Ranch Hand

Joined: Sep 21, 2005
Posts: 34
>>Thank you for your answer. I have one more query, how to make a servlet ThreadSafe?<<



Implimenting the ThreadSafe interface is the one way.It may create difficult to access the resources in the multitaking process so better to go for syncronisation about the specific details which u want to be threadsafe. this way is most prefered one.


Kamlesh
Be oN edGE SCJP 1.4, on edGE for SCWCD 1.4
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I agree with the previous poster. For the most part, a good web app design implies that servlets have no state, so there are no thread issues.

What are you trying to do?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by kamlesh tayade:
>>Thank you for your answer. I have one more query, how to make a servlet ThreadSafe?<<



Implimenting the ThreadSafe interface is the one way.It may create difficult to access the resources in the multitaking process so better to go for syncronisation about the specific details which u want to be threadsafe. this way is most prefered one.


kamlesh t

You appear to have changed your screen name from an valid one to an invalid one.

Please change it back before posting to this site again.
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 382
    
    2
Hello Jeff,

I had a doubt about single Thread model. Creating a single instance of a servlet. so am I correct in assuming that I have to sinchronize my methods if I want to avoid multiple threads from accessing it ?

Thank you. I appreciate all your answers.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
What are you doing in your servlet that is causing you to worry about thread safety? This is not been an issue for any servlet I've ever written.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
am I correct in assuming that I have to sinchronize my methods if I want to avoid multiple threads from accessing it ?

No, you simply have to use the mechanisms provided by the servlet API to keep each user's data separate.
The programming conventions you may have used for single-user desk top applications no longer apply and you will have to learn new ones. For example, forget about loading your servlet class with instance variables and methods using instance variables.
Bill
Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Originally posted by Ayub ali khan:
Hello Jeff,

I had a doubt about single Thread model. Creating a single instance of a servlet. so am I correct in assuming that I have to sinchronize my methods if I want to avoid multiple threads from accessing it ?

Thank you. I appreciate all your answers.



Servlet Basics

Servlets have three methods init(), service() and destroy(). Out of these init() is called only when the servlet instance is created. Service() method is a thread safe method which is called everytime a new request is recieved. Destroy() method is called only once when the servlet instance is removed.

Hence anything in init() and destroy() is not thread safe and you will have to use synchronization to make it thread safe.

-Manhar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Originally posted by Manhar Puri:

Hence anything in init() and destroy() is not thread safe and you will have to use synchronization to make it thread safe.


Actually, it's exactly the opposite.

Since the init() and destroy() methods are called only once on an instance of a servlet, there's no chance that more than one thread will call the methods simultaneously. Hence, no thread safety issues.

The service() method, on the other hand, can be called from mutiple threads once the servlet is placed into service, and so, any non-thread-safe activity will need to be synchronized.

That said, well-designed servlets rarely need to perform synchronization of any kind. If you find yourself having to synchronize a lot, it may be a sign that you need to re-think your design.
[ June 13, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Originally posted by Manhar Puri:



Servlet Basics

Servlets have three methods init(), service() and destroy(). Out of these init() is called only when the servlet instance is created. Service() method is a thread safe method which is called everytime a new request is recieved. Destroy() method is called only once when the servlet instance is removed.

Hence anything in init() and destroy() is not thread safe and you will have to use synchronization to make it thread safe.

-Manhar



okay a few mistakes in above post. Firstly you do not need to synchronize on local data or parameters, so ignore comments on init() and destroy() methods.

Yes, if the methods you have created in your servlet access any instance variables the this access needs to be synchronized.

-Manhar.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Yes, if the methods you have created in your servlet access any instance variables the this access needs to be synchronized.

Not exactly - I use instance variables all the time BUT they are effectively read only. If no request Thread can modify an instance variable then access does not have to be synchronized.
I agree with Bear - if you find yourself having to synchronize a lot, you need to take a step back and look at your over-all design.
Bill
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Manhar Puri:

okay a few mistakes in above post.


A few??? How about the whole post! Even the statement "Servlets have three methods init(), service() and destroy()" isn't strictly true. Take your lumps, lad
 
 
subject: single instance of servlet (interview question)