> From: Nathan M. Andelin
>
> I've heard that Websphere manages a number of components in addition to
> Servlets.  The session object may be a good example.  Do
> programmers need to
> synchronize calls to session methods, for example.  Or are a number of
> things like that automatically synchronized?

Synchronization is not required in order to be threadsafe, provided you
access only threadsafe variables.  Instance variables in your servlet class
are not threadsafe, since there is only one copy of the variable shared
among all the threads executing that servlet.  On the other hand, variables
passed on the stack (e.g., as parameters to methods) are stored on the
stack, not in the instance, so are threadsafe.

This being the case, when a servlet method (as I said, usually doGet or
doPost) is invoked, it is passed two thread-specific variables, the
HttpServletRequest and HttpServletResponse.  Invoking the getSession method
of your thread-specific HttpServletRequest object gives you a
thread-specific (and thus threadsafe) HttpSession object.

By attaching your own working storage class to the HttpSession object, and
accessing only variables contained within that object, you are now
threadsafe, without the penalty of synchronization.  This is sort of a rule
of thumb for any type of multi-threaded programming - store it on the stack
(or on the heap).

So, the upshot of this whole thing is that servlets should avoid instance
variables.  If you must have one (perhaps for inter-session communication),
access to it must be synchronized.  But in general you can avoid
synchronization by using threadsafe variables stored in the session object.

Joe



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.