File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes single thread or multi thread ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "single thread or multi thread ?" Watch "single thread or multi thread ?" New topic

single thread or multi thread ?

sim sim
Ranch Hand

Joined: Jun 05, 2002
Posts: 55
I have been asked this question in an interview. With respect to servlets when do I go for multithreaded programming or single threaded.
Sim Sim
Dakshinamurthy Ramachandran

Joined: Mar 09, 2002
Posts: 16
Basically servlets are multithreaded by design. Meaning single object instance of servlet will serve all the clients. In a specific scenario, when you expect concurrency will affect the integrity of data, you can go for single threaded servlet by implementing Single Thread Model interface.
Hope this answers your question.
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Is this correct? Single Thread Model has the impact on performance.
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Yes, single thread model criples performance as the servlet has to complete servicing a request before servicing another one (it can't service more than one request at a time).
However, this does not mean that only one servlet will be loaded. The most common behavior is for the servlet container to instantiate more than one instance of the servlet each for servicing a particular request. This kills performance as every instance has to be initialized and then destroyed for every request without counting the effect of many instances loaded in memory. If you have thousands of such instances, your server will take a hit.
Synchronization will still be an issue if those instances have to access common external (external to the servlet) resources.
[ February 05, 2004: Message edited by: Brahim Bakayoko ]

SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="" target="_blank" rel="nofollow"></a> down for a while...
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
Single Thread Model degrade the servlet performance dramatically, it is just
a reference Model for Multithreaded Model.
In reality, it is not much of use.
Pay attention to the following synchronization issues when you write a multithreaded servlet service:
a. access servlet instance variables
b. access session shared data
c. access application shared data
I agree. Here's the link:
subject: single thread or multi thread ?
jQuery in Action, 3rd edition