SUBSCRIBE JSP-INTEREST Lam wrote:

> Hi: Some concurrency issues that are bugging me:
>
> Assuming that I have something like this in my jsp:
>
> application.setAttribute("TableColour", tableColour);
>

In this particular case, the answer is "the last one to set it wins".  The attributes 
API has the same semantics that a Hashtable does -- any
locking required to prevent corruption of the internal data structures happens inside 
the "application" object.

While this is typically the case in Java, it is not universal -- for example, the Java 
collection classes API added in JDK 1.2 is explicitly
*not* thread-safe.  The reasoning was that many apps run in a single threaded 
environment, or in a context where the application has already
locked things.  Removing the internal locking makes the collections classes run 
faster, at the cost of requiring the user of these collections
to do their own locking.

Swing is another case where there are documented restrictions on what you can do in 
which threads.

>
> What if another thread is executing the same bit of code?
> Is there any concept of 'locking' to prevent other threads getting hold
> of "TableColour" while I'm writing to it?
>

The only guarantee is that the reader (in one thread) will not see it unless the 
writer has written it.  In the case of two writers, the
guarantee is that there will be only one copy (because that's the way the API is 
defined), so there is no possibility that a reader could ever
see two objects with the same key.

>
> Also, if I have something like this:
>
> <%@ include file="test.html" %>
>
> If I'm running a site with lots of concurrent users, are there any problems with 
>users trying to access the test.html file at the same time?
>

This isn't actually a good example of your question, because <%@ include 
file="test.html" %> is processed when the JSP page is compiled, not
when it is executed.  However, let's generalize the question a little:

    "Is there any problem with a particular
    file being read by multiple threads at
    the same time?"

The answer, for essentially all current OSs, is that there's no problem as long as 
each thread opens their own FileInputStream (or whatever)
object to read the file.  For writing, you should provide your own application-level 
locking for particular portions of the file -- which, if
you think about it, is exactly what a database system does for you.

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to