--
Your answer is simple and complex. A stack variable is never a shared
resource unless you make it so by passing it around to other running
servlets (what a concept), or into a shared resource manager like a database
connection pool.
Before I get flamed... I understand that the "stack variable" concept
doesn't apply directly in Java, but the intention here is to point to any
variable that is created for this execution/instance such as locally defined
automatic variables.
If your String variable is local to the function handling the request then
each user will see the DESIRED result. This is true regardless of how many
threads are running through this function since they each have their own
stack and therefore separate instances of that string.
That is they will see their user name.
If the variable belongs to the CLASS that is handling the request and is
non-static and it is SingleThread model, then they will also get the DESIRED
result since the function can be executing only once at a time, there is no
chance if inadvertantly overwriting for a given INSTANCE of your variable.
If the variable belongs to the CLASS and is static, even the SingleThread
model presents a chance of overwriting since all instances share that
variable. And even though no one instance may be executing simultaneously,
there is no assurance that you will not have multiple instances of your
class running at the same time.
If the variable belongs to the CLASS and it is not single thread model, you
will certainly get the wrong result with frequency.
Hope this clears it up. What it all boils down to is that SingleThreadModel
only protects any given Instance of your servlet from executing
simultaneously, and you need to take into account all the same things as
when you write an applet or program using multiple threads.
For our purposes we use mainly static methods and stack variables to
minimize how much we need to think about threads and only deal with
synchronizations when we have to access a pool of database connections or
some other shared resource.
----- Original Message -----
From: Joshua Slack <[EMAIL PROTECTED]>
To: Java Apache Users <[EMAIL PROTECTED]>
Sent: Tuesday, June 29, 1999 10:12 AM
Subject: Any easy answer?
> --
>
> After reading the FAQ and the Java tutorial (thanks Nimret) I still need
this
> question answered:
>
> If I have the following piece of code: (let's just make it pseudo code)
>
> String y = ""
> put user's name into String y.
> print String y
>
> If a user accesses the servlet and gets to the second line, setting y to
be
> "John Doe", then another user access the same servlet's first line
(setting y
> to "") before the first can print the string, would the first user then
print
> "" instead of "John Doe"?
>
> I read all about Single Threaded, Synch and Asynch Threads, but they talk
> about potential problems due to shared resources (they give databases as
> their example)... Are variables a shared resource too?
>
> Thanks for your indulgence,
>
> -- Joshua Slack
>
>
>
> --
> --------------------------------------------------------------
> To subscribe: [EMAIL PROTECTED]
> To unsubscribe: [EMAIL PROTECTED]
> READ THE FAQ!!!! <http://java.apache.org/faq/>
> Archives and Other: <http://java.apache.org/main/mail.html/>
> Problems?: [EMAIL PROTECTED]
>
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]