This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single Thread Model

 
Parag Kale
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the SingleThreadModel Interface does not have any methods, how does it work ?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Parag Kale
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Peter.Got it.
regds,
Parag
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic