• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Whether HTTPServlet Extends Thread or Not?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have following doubt. First of all i wnat to know whether Any way servlet extends java.lang.Thread class or Runnable Interface? This is just because as far my knowledge of understanding of Servlet, For each Client request There will be thread created and not Instance of an object? Am I correct? If suppose a thread is created then there should be some way of implementation of runnable interface?
Where does that implementation comes? Can any clarify me.
Thanks for your help.
Regards,
R V Holla
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet may be used by more than one thread, but that doesn't mean that a servlet is a thread. A thread may instantiate and use any Java classes.
As a simpler example, consider the following code:

Each "Thing" is a thread, and the Thinger runs 10 of them at once. Each thread creates a String and uses a method it provides. It is important to notice that the String class is not a Thread, and it doesn't implement Runnable, yet it is used by many threads at once.
A servlet container also creates several threads, (for a simple example, one for each request). Each of these threads uses whatever class and object instances it needs (such as your servlet class as well as classes for things like Request and Response objects) to service the request, but these classes and objects it uses are not themselves threads.
 
Raghavendra Holla
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Frank,
Thanks for your help...
According to you, Servlet Container Will have several Threads of its own , and these threads can create as Many instances 0of Servlet class as required depending on multiple client request depending on availability of already created instances? Am I understood clearly?

But when I printing Servlets HASH code by multiple clients Request they are printing same Hash code. That menas there is only one instance of Servlet class available. Am I talking stupid?
Why i am asking all these details because, which ever book on Servlet you take when they compare CGI to Servlet, They tell CGI creates multiple process , Servlet doesn't. I don't how much a instance creation is advantage from a process..
Regards,
Holla.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet container is free to create as many or as few instances of your servlet class as it likes. Almost all servlet containers only create a single instance of your servlet class (*), which may be used simultaneously by many threads. This is why your hash code is the same - it is the same instance of your servlet code, but being used by different threads.
(*) Unless your servlet is marked as "implements SingleThreadModel" which is not a good idea unless you really, really, need it.
 
Raghavendra Holla
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Frank,
Thanks a lot.. Things are clear now.. Sorry if i troubled you lot.
Regards,
R V Holla.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem. Glad to help.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
A servlet container is free to create as many or as few instances of your servlet class as it likes. Almost all servlet containers only create a single instance of your servlet class (*), which may be used simultaneously by many threads. This is why your hash code is the same - it is the same instance of your servlet code, but being used by different threads.
(*) Unless your servlet is marked as "implements SingleThreadModel" which is not a good idea unless you really, really, need it.


You are almost correct. A servlet container is not free to create as many instances of a servlet as it wants. The servlet container is constrained by the servlet specification. Here is what the V2.3 spec says:
"2.2 Number of Instances
"In the default case of a servlet not implementing SingleThreadModel and not hosted in a distributed environment, the servlet container must use only one instance of a servlet class per servlet definition.
"In the case of a servlet that implements the SingleThreadModel interface, the servlet container may instantiate multiple instances of that servlet so that it can handle a heavy request load while still serializing requests to a single instance.
"In the case where a servlet was deployed as part of an application that is marked in the deployment descriptor as distributable, there is one instance of a servlet class per servlet definition per VM in a container. If the servlet implements the SingleThreadModel interface as well as is part of a distributable web application, the container may instantiate multiple instances of that servlet in each VM of the container."
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. Yes, they do seem to have tidied that up in the recent versions of the spec. It does seem strange to restrict the servlet container to use only one instance though. Presumably it's so instance-variable hit counters and the like are guaranteed to work.
I shall modify my reply next time I answer a question like this.
Thanks.
 
Raghavendra Holla
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank and Kevin...
Regards,
R V Holla.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic