Howdy,
>I have recently been reading the Java Language Specification (JLS)
>concerning threads, locks and the Java memory model
>
>http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html#
3020
>6
>and the book excerpt "Synchronization and the Java Memory Model" from
>"Concurrent Programming in Java" (Doug Lea):
>http://gee.cs.oswego.edu/dl/cpj/jmm.html
Oh you went right to the source. This is both good and bad: good
because you're reading the complete, definitive resource. Bad because
it can be confusing when it comes to real life, as containers take care
of many of these details for you.
>(1) If I use the Sevlet init() method to initialize some variables in a
>web application and then access these variables from a service()
method,
>must I synchronize the setting of the variables from init() and the
>getting of these variables from service() in order to guarantee that
the
>thread running the service() method can see the changes made to the
>variables by the init() method on a different thread?
No, you need not synchronize this, because
>Alternatively is
>this looked after in some way by Tomcat to ensure that the servlet
>initialization is visible to requests to the servlet.
The container takes care of it for you (not just tomcat) by how it
implements the servlet lifecycle.
>(2) I have the same question regarding the use of the ServletContext
>object: must I synchronize access to all attributes that I set/get in
the
>ServletContext to guarantee that changes made by one thread are visible
to
>another?
This is only slightly more interesting: you don't need to synchronize
ServletContext access normally. If you have a situation where multiple
threads (e.g. multiple concurrent requests to the same servlet) would
write into the ServletContext (reading is fine), then you might need to
synchronize because you could have a race condition on the write.
Yoav Shapira
This e-mail, including any attachments, is a confidential business communication, and
may contain information that is confidential, proprietary and/or privileged. This
e-mail is intended only for the individual(s) to whom it is addressed, and may not be
saved, copied, printed, disclosed or used by anyone else. If you are not the(an)
intended recipient, please immediately delete this e-mail from your computer system
and notify the sender. Thank you.
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]