RE: Java memory model and web applications

2003-11-20 Thread Shapira, Yoav

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]



Java memory model and web applications

2003-11-20 Thread Mark . Shotton
Hello

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#30206
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

I am now very puzzled as to how the threading issues described in these 
articles relate to a web application.

The articles state that:
JLS: Each thread has a working memory, in which it may keep copies of the 
values of variables from the main memory that is shared between all 
threads.  Changes to variables made by one thread in its working memory 
are only guaranteed to be copied back into main memory when the thread 
performs an unlock action on any lock, or the thread terminates.  A thread is only 
guaranteed to load a 
variable from the main memory into its working memory when it performs a 
lock action or the first time it accesses the variable.
Doug Lea: "Changes to fields made by one thread are guaranteed to be 
visible to other threads only under the following conditions: A writing 
thread releases a synchronization lock and a reading thread subsequently 
acquires that same synchronization lock."

Could anyone please answer the following questions relating to a web 
application:

(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?  Alternatively is 
this looked after in some way by Tomcat to ensure that the servlet 
initialization is visible to requests to the servlet.

(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?

Thanks for your help.

Mark