• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version of Struts are you using where Action extends HttpServlet?
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amy,
Just to correct you on your thoughts,
ActionServet is the only action which is servlet among all the action classes struts provide.
 
reply
    Bookmark Topic Watch Topic
  • New Topic