This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes is abstract class instance variable in servlet container - thread-safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "is abstract class instance variable in servlet container - thread-safe" Watch "is abstract class instance variable in servlet container - thread-safe" New topic
Author

is abstract class instance variable in servlet container - thread-safe

Amy Saari
Ranch Hand

Joined: Sep 13, 2007
Posts: 30
Hi All

I need some help if you have a minute. We have an app in production that's behaving poorly. We know we have a NOT thread safe condition where two threads running through the same servlet are looking at the same value in an instance variable (userName in the example below). So, for example, two threads in the class first are looking at a single copy of userName.

But what about this - can one thread in first and one thread in second be looking at the same instance of userName? if baseclass is abstract, then are there two instances of userName? one when new is called for class first and one when new is called for class second by the servlet container? know what i mean?

Here's some simple pseudo code to illustrate :




thanks for any thoughts,
amy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

If the class is a servlet there will be only one instance shared across all threads and there will be trouble. Write servlets in a thread-safe manner always.
Amy Saari
Ranch Hand

Joined: Sep 13, 2007
Posts: 30
Hi Bear,

I wish you had not moved this to Struts - it doesn't have anything to do with Struts (I should have just left the Action off and put in HttpServlet). Can you / would you move it back perhaps?

We know it's not thread safe. The question is this:

If two classes extend an abstract class that extends HttpServlet - how many instance of an instance variable in the abstract class exist? One or two?

Thanks,
Amy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

There will be one instance of the variable per instance of the class. The fact that it was declared in an abstract superclass is moot.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Which version of Struts are you using where Action extends HttpServlet?
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Hi Amy,
Just to correct you on your thoughts,
ActionServet is the only action which is servlet among all the action classes struts provide.


SCJP 1.5(97%) My Blog
 
 
subject: is abstract class instance variable in servlet container - thread-safe