On Tue, 28 Sep 1999, Shandir Ilis Thane Maeir wrote:
> "Cote, Stephane" wrote:
>
> > What is the best way to make a servlet Thread safe.
>
> Impelment interface SingleThreadMode.
Not really. First of all, he said "best" way, not "easiest" way.
Using SingleThreadModel incurs some performance penalties, so it may
not meet his criteria for best.
Perhaps more importantly, while implementing SingleThreadModel may be
easy, it actually doesn't guarantee that a servlet will be thread-safe
-- it only guarantees that only one thread at a time will run through
a *particular* instance of a servlet. But several instances may be
created (in fact, this is how servlet engines typically handle
SingleThreadModel, to help ease some of the performance penalties), so
you can still have multiple threads running the same code. You may
not need to worry about instance variables, but you still need to
worry about class variables (i.e. statics) and other shared resources
(e.g. databases, files).
This has been discussed many times before on the list, I suggest you
check the archives. It is not really a simple topic (entire books
have been written about threads/concurrency).
I think the best thing is to design your code to avoid areas where
contention can be a problem, and if there are still possible trouble
areas, use synchronized on the smallest blocks possible so performance
is as minimally affected as possible. Generally, avoiding things like
instance and class variables (use locals and pass parameters instead)
is a good idea. And you need to watch out for other shared resources.
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html