Link to home
Start Free TrialLog in
Avatar of qgai
qgai

asked on

servlets single thread vs multiple threads

I am confused about servlet single thread vs multiple thread. Could somedody help me clarify these questions?
1) When should I use single thread or multhreaded servlets?
2) What are the advantages and disadvantages of using single threaded vs multithreads servlets?
3) What is the cachsing issue and how to resolve it?
Your expertise is greatly appreaciated!
Thanks in advance!
Qiwei
Avatar of joeltt
joeltt

Paraphrased from white paper...

SingleThreadModel is a tag interface that a servlet can implement to transfer its reentrancy problem to the servlet engine.

Often this is accomplished by the servlet engine creating a separate servlet instance for each user. Because this creates a great amount of system overhead, SingleThreadModel should be avoided.

SingleThreadModel is often used to protect updateable servlet instances in a multithreaded enviornment. The better approach is to avoid using servlet instance variables that are updated from the servlet's service method (use init() when possible).

J
>> 1) When should I use single thread or multhreaded servlets?
Ok as Joeltt said, single thread model is a tag interface and is just an indication that the servlet follows a SingleThreadModel. Usually, u have servlet instance that is accessed by more than one thread at any given point of time. There might be cases that you want each request to servlet from different client to be handled in separate servlet instance itself. Then you wud go in for single thread model. In such a case you have as many servlet instances as are the requests to it.

>> What are the advantages and disadvantages of using single threaded vs multithreads servlets?
Well, this is one primary advantage that servlets had over CGI programs that do the same thing. For each request made to a CGI program it starts a new process to handle it. It can be really resource-extensive, as you can see. Usually u go in for multi threaded model. This offers you a lot many benefits that it is one instance of servlet that is created to handle all the requests to it.

>> What is the caching issue and how to resolve it?
Well, caching is a time-tested concept to enhance your perfomrance. Usually u cache info that we need to be re-cycled amongst different components that make up our application. Issues are that in case u'r caching info is big then it can cause your application performance to degrade rather than improve. Usually, u use browser cache to store info. But caching can also imply that yo store objects that you create in some kind of collection like hashtable to re-cycle them as and when required. I hv used such a concept. So, you can also go ahead and use it. Hope this helps.
 
Bye and regards
Sumit
Avatar of qgai

ASKER

Thanks all.
Since in multithreaded servlets, all the user shares the same instance(if I understand correctly), the instance varibles will also be shared by all the clients. This is apparently a problem. The approach is handle this is to avoid using instance variable in multithreaded servlets?
Also, if I am writting a servlet, how can I specify it is a singlethreaded or multithreaded servlet?

Thanks so much.
By Default HttpServlet is Multi Threaded .

For Single Threaded Servlets ,
just implement the SingleThreadModel Interface .

Hope this helps .

ASKER CERTIFIED SOLUTION
Avatar of sumit_only
sumit_only

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial