No. They can't be, unless their accesses are synchronized.
Only local variables in methods are thread safe because local variables
are allocated on the stack per each call.
> > "...the instance , static variables used in a servlet reference
> > object are not thread safe..."
[quoted text clipped - 26 lines]
> Only local variables in methods are thread safe because local variables
> are allocated on the stack per each call.
if i make synchronized setter/getter methods to access those instance
variables , will that be thread safe now ?
hiwa - 11 Nov 2006 00:03 GMT
> > > "...the instance , static variables used in a servlet reference
> > > object are not thread safe..."
[quoted text clipped - 29 lines]
> if i make synchronized setter/getter methods to access those instance
> variables , will that be thread safe now ?
Yes.
Simon Brooke - 11 Nov 2006 13:52 GMT
>> > > "...the instance , static variables used in a servlet reference
>> > > object are not thread safe..."
[quoted text clipped - 30 lines]
>> variables , will that be thread safe now ?
> Yes.
Errr... no. And if you think it is, you /really/ don't understand how a
Servlet engine works.

Signature
simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; Quidquid latine dictum sit, altum sonatur.
Lew - 12 Nov 2006 15:18 GMT
>>>>> "...the instance , static variables used in a servlet reference
>>>>> object are not thread safe..."
[quoted text clipped - 6 lines]
>>>>> could you please explain ...why that statement is correct ?
>>>>> this x,y cant be thraed safe ?
...
>>> if i make synchronized setter/getter methods to access those instance
>>> variables , will that be thread safe now ?
>> hiwa wrote:
>> Yes.
> Errr... no. And if you think it is, you /really/ don't understand how a
> Servlet engine works.
The servlet engine may well re-use the same instance of a servlet class to
handle multiple requests. The instance refers to its own private variables
without use of accessor methods. Since multiple threads may be using the
instance, problems might ensue.
Synchronizing access to a shared resource involves synchronizing on the
resource or one that is tied to it, as "synchronized(this)" or
"synchronized(x)". Synchronizing to the accessor methods is not reliable, as
it ignores accesses that bypass those methods.
It's better to stick with stack variables for servlet action.
- Lew
Daniel Pitts - 11 Nov 2006 01:33 GMT
> > > "...the instance , static variables used in a servlet reference
> > > object are not thread safe..."
[quoted text clipped - 29 lines]
> if i make synchronized setter/getter methods to access those instance
> variables , will that be thread safe now ?
Or, if the referenced objects are threadsafe (String objects are), then
you can use AtomicReference.
Mark Jeffcoat - 11 Nov 2006 09:45 GMT
> if i make synchronized setter/getter methods to access those instance
> variables , will that be thread safe now ?
No. Though I understand how 'yes' is tempting. Because
it would surely be easier that way.
class Subtractor {
private int subtrahend;
private int minuend;
public synchronized int getSubtrahend() {
return subtrahend;
}
public synchronized void setSubtrahend(int s) {
this.subtrahend = s;
}
public synchronized int getMinuend() {
return this.minuend;
}
public synchronized void setMinuend(int m) {
this.minuend = m;
}
public synchronized int difference() {
return minuend - subtrahend;
}
}
Let's say you have multiple threads
accessing the same Subtractor object.
Let's call them Thread 1 and Thread 2, because
I'm writing this at 3:30 AM.
This is a possible flow:
Thread 1: setMinuend(5)
Thread 1: setSubtrahend(2)
Thread 1: difference() == 3
Thread 2: setMinuend(10)
Thread 2: setSubtrahend(8)
Thread 2: difference() == 2
Hooray!
Unfortunately, this is also a possible
outcome:
Thread 1: setMinuend(5)
Thread 1: setSubtrahend(2)
Thread 2: setMinuend(10)
Thread 2: setSubtrahend(8)
Thread 1: difference() == 2
Thread 2: difference() == 2
Ooops.
Why didn't the synchronized voodoo help? It did it's job--
no two threads were allowed to access a getter or setter
at the same time. Unfortunately, that's completely pointless.
We only get the right answer if setMinuend(), setSubtrahend,
and difference() aren't all called by the same thread without
any interruption.
Go forth and sin no more.

Signature
Mark Jeffcoat
Austin, TX
Simon Brooke - 11 Nov 2006 13:51 GMT
>> No. They can't be, unless their accesses are synchronized.
>> Only local variables in methods are thread safe because local variables
>> are allocated on the stack per each call.
>
> if i make synchronized setter/getter methods to access those instance
> variables , will that be thread safe now ?
No. You /cannot/ use instance variables of the Servlet dynamically in
serving requests. At all. Ever. You can use them to hold configuration
data. You can also create separate per-service objects in which to pass
around the context of the request (after all, this is essentially what
HttpServletRequest and HttpServletResponse are, and there's nothing to
prevent you creating a class of your own); and you can use the instance
variables of these per-service objects if you want to.
But you cannot use instance variables of the Servlet, because you don't
know how different threads will interleave their actions.

Signature
simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; Life would be much easier if I had the source code.