File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Single Thread Model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Single Thread Model" Watch "Single Thread Model" New topic
Author

Single Thread Model

Parag Kale
Greenhorn

Joined: Apr 08, 2001
Posts: 25
If the SingleThreadModel Interface does not have any methods, how does it work ?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Parag Kale:
If the SingleThreadModel Interface does not have any methods, how does it work ?

By simply being there. It's a tag interface, just like java.io.Serializable, java.lang.Comparable, java.lang.Cloneable, java.lang.Runnable, etc. Somewhere inside the servlet engine there's code like (not literally, of course, but functionally equivalent to):

C'est tout.

- Peter

[This message has been edited by Peter den Haan (edited April 24, 2001).]
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
What's ur point being? When ur servlet implements SingleThreadModel interface, the service method will process request one at a time but not concurrently.
If u declare no method in the servlet...then nothing happens..


SCJP 1.5, SCEA, ICED (287,484,486)
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
What peter says is right.you can say that implementing SingleThreadModel makes the whole servlet code "Synchronized" by the use of all the threading properties.It allows one request at a time only.
I thnik what peter has put in is the best way to explain.Thanks peter.


VAIBHAV <BR>SCJP
Parag Kale
Greenhorn

Joined: Apr 08, 2001
Posts: 25
Thank you Peter.Got it.
regds,
Parag
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
Peter's explanation is right on target, although strictly speaking java.lang.Comparable and java.lang.Runnable do define methods.
Whenever I see SingleThreadModel, however, the red flags go up. Almost invariably it is an attempt to plug holes in a faulty design. The only real advantage of SingleThreadModel (or the equivalent JSP page directive isThreadSafe="false") is that it prevents two requests from modifying an instance variable at the same time. But you shouldn't be using instance variables in a servlet anyway - there is no reason to do so. You can use local variables within the doGet() method and they will be automatically threadsafe.
In other words, instead of this:

do this:

If you need to pass a number of local variables between subroutines, you can define a structure class to hold them, define a local variable of that type, and pass that as a parameter between methods:

If you think you need to use instance variables for database connections or other things that need to persist between requests, you are headed for trouble. What you probably want is an HTTP session, instead.
At the heart of the problem is that SingleThreadModel results in multiple instances of the servlet running, each with a single thread. The default threading model has a single instance of the servlet and multiple threads running it. If you have an external resource like a database, and you need to synchronize access to it, SingleThreadModel will not have any effect other than to slow everything down.
------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Phil Hanna:
Peter's explanation is right on target, although strictly speaking java.lang.Comparable and java.lang.Runnable do define methods.

Yes, of course they do. How silly of me.
Completely agree on the red flags going up. In addition to storing things that really belong in the HttpSession, the SingleThreadModle is sometimes used because the servlet/JSP holds on to some non threadsafe resource, for example, a database connection. Indeed the fact that a server will instantiate your servlet multiple times gives you a kind of primitive connection pool. But in those cases you are much better off with a real, application-scoped connection pool.
- Peter

[This message has been edited by Peter den Haan (edited April 24, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Single Thread Model