File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Single Thread Model 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 "Single Thread Model" Watch "Single Thread Model" New topic
Author

Single Thread Model

Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
Hi,

I was going thru Single Thread model in Servlets 2.4 Spec.

Can anyone help me explaining this point:

"The use of SingleThread Model interface guarentees that only one thread at a time will execute in a given servlet instance's service method. This guarentee applies to each servlet instance, since container may choose to pool such objects. Objects that are accesible to more than one servlet instance at a time such as HttpSession, may be available at any particular time to multiple servlets , inclusimg those that implement SinglethreadModel".

How container handle such thing??

Thanks,
Anju Sethi


thanks,<br />Anju Sethi
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
First of all SinglethreadModel is not used anymore.Because it provides poor solution.You should never use SinglethreadModel.
if servlet implements SinglethreadModel then only single thread access servlet at a time.
But if there is request from other thread then SinglethreadModel will not block it
SinglethreadModel is used if you have single servlet model.
For example-:
if there is ServletA and ServletB both implementing SinglethreadModel.
then thread from ServletA will stop another thread from ServletA only
if there is thread from ServletB that will not be blocked.
i.e threads from same servlet will block each other only.
Another problem with SinglethreadModel is that it doesnot synchoronized ServletContext and HttpSession attributes.
Implementing SinglethreadModel is equal to synchoronized your service method.
You should always synchoronized on HttpSession and ServletContext object for HttpSession and ServletContext attributes.
HttpRequest and local variables(inside service method) are by default thread safe.
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

Forget about SingleThreadModel, it is no longer on the exam !


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Alim Atar
Ranch Hand

Joined: Oct 06, 2006
Posts: 102

Hey Thanks Gowher Good Explantion

I want to confirm that really this topic is not on exam ..?? :roll:


Regards,
Alim

SCEA/OCMJEA Part1,SCBCD1.5,SCWCD1.4,SCJP1.4,SQL(Z007)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41184
    
  45
Hello Alim-

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.


Ping & DNS - my free Android networking tools app
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
I want to confirm that really this topic is not on exam ..??


hmm... keep this in mind -

the 'page' directive has a attribute called 'isThreadSafe'. Its default value is true..this mean telling the container 'hey container, my servelt is TRUEly thread safe, and i dont need you to BABY-SIT and protect it by implementing the SingleThreadModel. if u say 'false' you are telling the container to explicaitly implement the SingleThreadModel, and its evil to do so

and if you make your servlet, implement it by saying

public class MyServlet implements SingleThreadModel {
...
..
}
then in that case the container will enure that only on threadoperated inside the servelt....that is the servelt will service only on request at a time....! but, if things go worse and number or rquests changes.... the container may create multiple instances of the same servlet to hande the requests, and this is evil !

correct me if i am wrong !


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
Thanks Gowher,

I am all the more confused.

1. "if servlet implements SinglethreadModel then only single thread access servlet at a time. But if there is request from other thread then SinglethreadModel will not block it. SinglethreadModel is used if you have single servlet model."

What kind of synchronization does SingleThreadedModel provide if it does not block another thread accessing Servlet at the same time. Why anyone would ever use it"

Please help
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
SingleThreadedModel does not block other thread at all.This is one of the reason why SingleThreadedModel should not be used.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41184
    
  45
But if there is request from other thread then SinglethreadModel will not block it. SinglethreadModel is used if you have single servlet model.

I think the second sentence is a bit misleading here (you can have multiple servlets, and the container can instantiate multiple instances of each servlet).

A second requests would not be blocked, but it would be serviced be a second instance of the servlet. So there is no need for synchronization.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
if servlet implements SingleThreadModel then there can have multiple instances in memory.if there are 5 threads accessing single servlet at a single time then there may be 5 instances of servlet present in memory.But it depends upon container.
Every container can support specific number of servlet instances at a single time and if there are more number of requests as compare to number of servlet instances supported by conatiner at a single time then some of the requests has to wait.
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
Thanks Gowher,

"if servlet implements SingleThreadModel then there can have multiple instances in memory.if there are 5 threads accessing single servlet at a single time then there may be 5 instances of servlet present in memory.But it depends upon container."

That is amazing. This helped me in understanding better.

Few more doubts to clarify more:

1. I believe all containers mentain a single instance of servlet to service multiple threads/requests. SingleThreadModel is an exception wherein multiple instances are created to service multiple threads/request. I am Correct ??
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
yes
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
In case servlet is not implementing SingleThreadModel then container keep single instance of servlet in memory and there is thread per request.
For example there are 5 requests at a single time then it means there are 5 runnable threads in memory which are accessing single instance of servlet.
Bryan Basham
author
Ranch Hand

Joined: Apr 30, 2001
Posts: 199
Hello all,

This is a great discussion... one that I always have with my servlet students. To summarize: there are three fundamental solutions for STM in the container.

First, there could be only one instance and the container queues each request (thread) to access the service() method. This is essentially equivalent to flagging the service method (or doGet, doPost methods) with the synchronized keyword. PROs: keeps the instance variables thread-safe. CONs: poor threading performance for high-volume of requests, does not keep static method thread-safe, does not keep session and application scopes thread-safe.

Second, the container could create one instance for each simultaneous request (one thread gets one instance); the container will create as many instances as is needed. PROs: keeps instance variables thread-safe. CONs: multiple instances means multiple copies of instance variables (breaks the semantics of variables that try to keep data about requests, such as a "access count"), does not keep static method thread-safe, does not keep session and application scopes thread-safe.

Lastly, the container could use a hybrid between the first and second technique where the container keeps a fixed-size pool of servlet instances. So if there are five instances and five simultaneous requests, then each request proceeds concurrently. However, if there are ten simultaneous requests, then five will get through and five will have to wait. The PROs and CONs are basically the same as the other two.

Ultimately, the true "evil" of STM is not any particular solution by the container but rather the servlet developer does not know which solution is used by a given container. Switching containers might mean recoding an STM servlet or bugs might appear. And for me, the worst issue is that STM only protects instance variables. It does not protect static variables, session, or application/context attributes. STM (if used naievely) provides a false sense of security. Thread-safe programming is hard and STM is not a quick fix.

I want to confirm that really this topic is not on exam ..??


There is no objective on the SCWCD exam about STM directly. However, there is an objective that tests your knowledge of which scopes are thread-safe. Now having said that I recall that during the exam creation workshop that one STM question did slip in. It's been a while since I took the Beta exam so I don't remember seeing this item. Does anyone (how has taken the exam) recall seeing a question specifically about STM?

Cheers,
Bryan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Single Thread Model
 
Similar Threads
Which of the following statements is correct regarding the servlets that implement
about SingleThreadModel?
SingleThreadModel Vs Concurrency
can same servlet have multiple instance in container
servlet pooling