File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Struts ThreadSafe

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement Threadsafe and singleton concepts in struts. Is it default or should we implement externally.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ram Charan
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic