servlet reference and thread safe

G

gk

"...the instance , static variables used in a servlet reference
object are not thread safe..."



why instance variables are not thread safe ?

say i have

class MyServlet extends HttpServlet
{
Sting x;
String y;

//doGet()

//doPost()

}


How , this x,y cant be thraed safe ?

seperate instance will have there own copy and hence they are not
going to be mixed up in multithreading enviornment.
so, these variables are thread safe .

could you please explain ...why that statement is correct ?
 
H

hiwa

gk said:
"...the instance , static variables used in a servlet reference
object are not thread safe..."



why instance variables are not thread safe ?

say i have

class MyServlet extends HttpServlet
{
Sting x;
String y;

//doGet()

//doPost()

}


How , this x,y cant be thraed safe ?

seperate instance will have there own copy and hence they are not
going to be mixed up in multithreading enviornment.
so, these variables are thread safe .

could you please explain ...why that statement is correct ?
this x,y cant be thraed safe ?
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.
 
G

gk

hiwa said:
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 ?
 
D

Daniel Pitts

gk said:
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.
 
M

Mark Jeffcoat

gk said:
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.
 
S

Simon Brooke

gk said:
"...the instance , static variables used in a servlet reference
object are not thread safe..."



why instance variables are not thread safe ?

Because the same instance may be doing different things with different data
in different threads.

Suppose you have a servlet (as, indeed, I do) which serves content from a
database. It may be called thus:

http://www.jasmine.org.uk/dogfood/story/article_31.html

or thus:

http://www.jasmine.org.uk/dogfood/story/article_42.html

The two calls must deliver different content. Suppose, in order to serve an
article, I stored the article index on an instance variable. Suppose the
request for article 42 comes in from Fred a few thousandths of a second
before the request for article 31 comes in from Joe.

So the servlet sets the instance variable to 42 in the thread that's
servicing Fred's request, and then goes to look in the database. But
before it can construct the query the thread that's servicing Joe's
request overwrites the instance variable with 31. So Fred gets sent
article 31, not article 42 as he requested.
How , this x,y cant be thraed safe ?

That's right.
seperate instance will have there own copy and hence they are not
going to be mixed up in multithreading enviornment.
so, these variables are thread safe .

Yes, but you don't have separate instances (normally). You have one
instance and separate threads. You can have separate instances but it's
very extravagant in terms of machine resources.
 
S

Simon Brooke

gk said:
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.
 
L

Lew

gk said:

Simon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top