You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Quaini Michele <mi...@fastweb.it> on 2000/07/07 22:28:36 UTC
OT: thread safety
Sorry for the OT
In the documentation of struts, is written that the
Action classes should be thread safe made
Is there documentation on the WEB or a good books where me
and my group can learn how to program thread safe classes?
Thanks
michele
Re: OT: thread safety
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Quaini Michele wrote:
> Sorry for the OT
>
> In the documentation of struts, is written that the
> Action classes should be thread safe made
>
> Is there documentation on the WEB or a good books where me
> and my group can learn how to program thread safe classes?
>
I don't know of any specific references (start searching for
"multithreading" and "multiple threads" and you should get some good
hits, but the most important principles related to Struts -- and to
servlet programming in general -- can be stated fairly easily:
* In a multiple thread environment, instance variables
of a class are shared across all threads, while local
variables (those declared inside a method) are not.
* Use instance variables of a servlet (or an Action class
in Struts) *only* to share read-only information (like the
action mappings table in Struts), or shareable resources
that manage their own thread safety (like a connection
pool).
* Use local variables for all information that is specific to
a particular request, as opposed to other requests that
might be executing at the same time.
* Request attributes (used to pass information on to the
final JSP page in Struts) are thread safe as long as there
are no references to these objects anywhere else (like
in an instance variable).
* Session attributes can be accessed by multiple threads
at the same time (example: a multiple frame presentation
will often cause multiple simultaneous requests that will
access the same session at the same time). You *may*
need to synchronize access to your session beans if they
can be modified by more than one thread at the same time
and do not have internal protections.
> Thanks
>
> michele
Craig McClanahan
OT: thread safety
Posted by Nick Afshartous <af...@iclick.com>.
Quaini Michele writes:
> In the documentation of struts, is written that the
> Action classes should be thread safe made
>
> Is there documentation on the WEB or a good books where me
> and my group can learn how to program thread safe classes?
If you can adhere to the guideline in the doc:
- don't use instance vars, only local vars (declared
in method scope)
then your Action classes will be thread-safe. For cases where
you must use instance vars then you must declare methods that
access these instance vars using keyword 'synchronized'.
See the attached slides for more background on concurrency and Java.
--
Nick