Forums Register Login

Single Thread Model

+Pie Number of slices to send: Send
If the SingleThreadModel Interface does not have any methods, how does it work ?
+Pie Number of slices to send: Send
 

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).]
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you Peter.Got it.
regds,
Parag
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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).]
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2015 times.
Similar Threads
Barn Burning Patterns
Jboss in Action question: migration from other Application Servers
WA #2 ..... word association
Need help understanding generics.
How low level do YOU code ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:55:42.