Shandir Ilis Thane Maeir wrote:
>
>
> "Cote, Stephane" wrote:
>
>>
>>
>> Hi all
>>
>> What is the best way to make a servlet Thread safe.
>>
>
> Impelment interface SingleThreadMode.
Actually, it's called SingleThreadModel -- but this only solves part of
the problem. For brevity, this is called STM in the remainder of this
discussion.
What STM guarantees you is that an instance variable in your servlet can
be used throughout a single request, without worrying about it getting
messed up by other simultaneous requests. It does not protect you at
all in cases where you try to save those values across requests, or use
them for shared resources that are modified by more than one request.
STM can also create a performance problem, depending on how you
configure your servlet engine.
A smarter strategy (IMHO) is to invest some time learning what
programming in a multi-threaded environment is like. Good places to
learn this are the Java Language Tutorial
(http://java.sun.com/docs/books/tutorial) and many introductory to
intermediate texts on basic Java programming. The servlet-focused books
will tend to spend some time on this issue as well.
The bottom line is that you need to assume that multiple requests will
be executing inside your servlet at the same time, and take suitable
precuations (using synchronization) to make sure that no two threads can
ever try to modify data items at the same time. It's hard to be more
specific than this without knowing the application details.
Craig McClanahan
___________________________________________________________________________
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