aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes SingleThreadModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "SingleThreadModel" Watch "SingleThreadModel" New topic
Author

SingleThreadModel

mayank jamindar
Ranch Hand

Joined: Aug 01, 2005
Posts: 38
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???
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
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


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
Ravi Appana
Greenhorn

Joined: Aug 25, 2004
Posts: 13
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.


Ravi Sankar Appana<br />SCJP 1.4
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
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

Joined: Aug 25, 2004
Posts: 13
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.
mani kanth
Ranch Hand

Joined: Nov 11, 2005
Posts: 57
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 ..

Radhika Gokhale
Ranch Hand

Joined: Dec 12, 2002
Posts: 40
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
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
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.


    SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
    Narendra Dhande
    Ranch Hand

    Joined: Dec 04, 2004
    Posts: 950
    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 ]

    Narendra Dhande
    SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
    Steven Colley
    Ranch Hand

    Joined: Feb 18, 2005
    Posts: 290
    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.


    SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
    Narendra Dhande
    Ranch Hand

    Joined: Dec 04, 2004
    Posts: 950
    Yes, you are right. Also The SigleThreadModel is deprecated in Servlet 2.4 spec.

    Thanks
    Steven Colley
    Ranch Hand

    Joined: Feb 18, 2005
    Posts: 290
    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

    Joined: Dec 04, 2004
    Posts: 950
    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

    Joined: Nov 11, 2005
    Posts: 57
    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.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: SingleThreadModel