• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

single instance of servlet (interview question)

 
Ranch Hand
Posts: 399
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for fun, try something like this:

... and you will see that AcmeServlet is being instantiated more than once.
 
Ayub ali khan
Ranch Hand
Posts: 399
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

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

Thanks and Regards

Ayub.
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 399
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic