• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is Struts ThreadSafe

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My interviewer asked , is struts Threadsafe(Is struts Action class thread safe ) . In google am getting different answers.
1. "No Struts is not thread safe " 2. Struts is not only thread-safe but also thread-dependent.
can you give me correct answer with reason.
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to implement Threadsafe and singleton concepts in struts. Is it default or should we implement externally.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Struts is a big framework, and the question of whether or not it's thread-safe can mean different things.

Struts Actions are thread-safe in Struts 2, and NOT thread-safe in Struts 1 unless you make them so (at least that's how I read http://struts.apache.org/2.0.14/docs/comparing-struts-1-and-2.html).

But even Struts 2 is a Java web app, and thus based on objects that are not inherently thread-safe (like HttpSession and ServletContext), so it's perfectly possible to create thread-unsafe web apps using Struts 2. Your code needs to take appropriate measures to guard against any concurrency issues when those are involved.

I generally recommend getting a good book on Java concurrency (the ones by Oaks/Wong and Doug Lea in particular); they're really indispensible for getting a good grounding in the issues and solutions Java provides.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's true. Struts1 action classes are not thread safe. You have to write thread safe action classes(Have only constants as instance variables).
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You have to write thread safe action classes (Have only constants as instance variables).


While that approach is sufficient for achieving thread-safety, it's not a necessary. The crucial aspect is that access to shared mutable state must be guarded. There's nothing wrong with using instance variable in servlets or Struts 1 actions, as long as they are read-only, or access to them is properly synchronized.

Note that there are other potential sources of concurrency problems besides instance variables, like sessions and context attributes.
 
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
This is one of those interview questions where I end up asking the interviewer more questions than s/he asked me in order to be able to actually answer their original question, because as asked, it's a little misleading. They may just be looking for the canonical answer, which in general, makes me not want to work for them.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic