• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

SingleThreadModel

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friend,
Please help me resolve correct or not:

The servlet container may create multiple instances of the servlet and dispatch each servlet request to a different servlet instance.
J2ee says YES to this.HFS says NO
How can container have many instances?

Also let me know does this Model, makes Instance variables thread safe?
How?
What is difference in Instance and class variables?
And Does this Model effect Final instance variables? ARe they thread safe before and after it???
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



The servlet container may create multiple instances of the servlet and dispatch each servlet request to a different servlet instance.
J2ee says YES to this.HFS says NO
How can container have many instances?


Can you tell me in which article you've found that J2EE spec says such thing? Because, the servlet container would always have one instance of a servlet class and all of the requests for the same servlet would go to the same object of that servlet. Moreover, if you have two kind of servlet mapping for the same servlet, you must have two different URL-mappings for them. So, there would be two instances now. BTW, servlet container uses multi-threading for request processing i.e. each request would spawn in a saperate thread.

If incase you've impression of implementing SingleThreadModel interface, then the servlet container would always create the servlet objects depending upon the requests. So, each request would have a saperate servlet instance with it. However it is not advisible to user SingleThreadModel rather code in such a way, that it should be thread safe.



Also let me know does this Model, makes Instance variables thread safe?
How?


After implementing the SingleThreadModel interface, you'd always have a separate servlet instance for each of the requests. So, there is no question of accessing the same object by many threads.



What is difference in Instance and class variables?


Instance variables are the variables defined at the class level. Such variables would have different copies as per the objects of that class. Class variables are static variables which would have only one copy across all the objects.



And Does this Model effect Final instance variables? ARe they thread safe before and after it???


Now, as I explained before that there would be multiple instances as per the requests if one implements SingleThreadModel, instance variables are always thread safe. Final variables are something which can't be changed once assigned a value. So, in any case final variables are not affected by threads.

I hope this helps
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover, if you have two kind of servlet mapping for the same servlet, you must have two different URL-mappings for them. So, there would be two instances now.

There would be only one servlet instance always, even if there are two URL-mappings for the same servlet. Once a container has loaded a servlet class and has initialized the servlet (can happen after the first request), after which any request for that servlet (irrespective of the URL mapping it came from), the Container allocates a thread for the same servlet instance that is already available.

Topic:SingleThreadModel Q: The servlet container may create multiple instances of the servlet and dispatch each servlet request to a different servlet instance.

If the SingleThreadModel (deprecated as per 2.4 Spec) is used, the container creates one instance of the servlet, and queues up the requests for that servlet, serving one client at a time.

It is advisable to not use instance variables in servlets.But, if the need is really there, you need to ensure that the state of the instance variables is maintained same across multiple requests.

Thanks,
Ravi.
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravi,



There would be only one servlet instance always, even if there are two URL-mappings for the same servlet.



I think this is not correct. Let me elaborate this. I meant by this:

There would be two instance of the same servlet class.



If the SingleThreadModel (deprecated as per 2.4 Spec) is used, the container creates one instance of the servlet, and queues up the requests for that servlet, serving one client at a time.


Where have you read that? Can you provide some material on that? I can provide one here.
Moreover, if you want to say that one request would be served at a time, what about the other requests? Lets say if one request is being served with an instance, the other would be queued regardless those clients have to wait for the time taken to serve for the prev. request??? If 100,000 requests come, they all have to be queued? It is clearely mentioned in the link I've provided that a pool of servlet instances is maintained which is fair enough.
 
Ravi Appana
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bimal,In the example you provided, how did you verify that there are infact two servlet instances loaded?

About the SingleThreadModel, I read from HFSJ. Also in the Techdocs link you have given, the statement is
"If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet"
Which means that the requests are queued up when there is a single servlet instance that implemented SingleThreadModel.
But, also the statement ended with an OR clause - "by maintaining a pool of servlet instances and dispatching each new request to a free servlet"
I am not sure how the container can be made to choose this.

Also, In the JSP specification first chapter, it is specified if a JSP's page directive has the "isThreadSafe" attribute set to false, the JSP container shall dispatch multiple outstanding client requests, one at a time, in order they were recieved, to the page implementation servlet for processing.

Please share your thoughts.

Thanks,
Ravi.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PLEASE ,clarify my doubt

if we implement single thread model in a servlet when multiple requests comes multiple instances or created is it correct what i understood ..

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mani,

As per the Servlet specs if a Servlet implements SingleThreadModel then the servler container may instantiate multiple instances to handle heavy request load.

So if a Servlet does not implement SingleThreadModel then there will be one instance per servlet declaration in DD but there will be multiple threads accessing this single instance . If a servlet implements SingleThreadModel then there may be more than one instances of the servlet as per container's wish but each instance can be accessed by one and only one thread at a time.

I believe this understnading is correct. Please let me know if I am missing out some point here.

Regards,
Radhika
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rhadika explanation is correct.

NO SingleThreadModel
  • ONLY ONE servlet instance.



  • SingleThreadModel
  • MAY have more than one instance;
  • ONLY one thread acces a specific instance at once.


  • And to finish, one reminder : SingleThreadModel is BAD.
     
    Ranch Hand
    Posts: 951
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    In addition to Radhika and fredric, in the case of servlet which is not implementing SingleThreadModel, then there is only one instance per VM. Thant means if the servelet running in distributed environment, there is more than one instance of servlet per container (application server), but only one instance per JVM.

    In case of default Servlet there is only one instance per container, as the default servelt is not distributed across the JVMs.

    Here the servlet instance releted to each <servlet-name> entry under <servlet> element in DD. So if the entries in the web.xml are




    Though we refering the same servlet class. the cintainer will create seperate instances for Name1 and Name2. We can define different <init-param> for above Name1 and Name2. In this case the ServletConfig is totally different in view of Servlet initial parameters.

    The <servlet-mapping> does not affect the number of instances, as it is only mapping to <servlet-name>. It should not create new instances.

    This is my understanding from the spec.

    Hope it help.


    Thanks
    [ March 27, 2006: Message edited by: Narendra Dhande ]
     
    Ranch Hand
    Posts: 290
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So just making clear regarding SingleThreadModel..wheter my Servlet implements a SingleThreadModel interface..

    The container CAN still use the "only one instance" for synchrozing the requests...

    or

    The Container CAN have a pool of instances and servers each new request with a new servlet instance.

    Does it make sense?

    tks.
     
    Narendra Dhande
    Ranch Hand
    Posts: 951
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, you are right. Also The SigleThreadModel is deprecated in Servlet 2.4 spec.

    Thanks
     
    Steven Colley
    Ranch Hand
    Posts: 290
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OK,tks Narendra.

    And st this time, another further info i also got , and would llike to make clear.

    If the environment is set as "distributable", so this is also applicable, right?

    - I mean, in a distributable environment + SingleThreadModel

    * The container may still have multiple instances of the same servlet for servicing

    OR

    * still may have a single servlet instance (and synch the requests). "even if the environment is set up as distributable",

    right? Does it make sense as well?

    Tks.
     
    Narendra Dhande
    Ranch Hand
    Posts: 951
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    Yes you are right. It is container dependent, weather to create single or multiple instances in case of SingleThreadModel. Only the instance is not process multiple request at time.

    Thanks
     
    mani kanth
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi thanks friends(frederic,radhika,narendra) for solving my issue ...
    please i have posted some more questions please see those posts and clarify
    those doubts also.

     
    Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic