aspose file tools*
The moose likes Struts and the fly likes Is Struts ThreadSafe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Is Struts ThreadSafe" Watch "Is Struts ThreadSafe" New topic
Author

Is Struts ThreadSafe

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
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

Joined: Sep 10, 2009
Posts: 293
How to implement Threadsafe and singleton concepts in struts. Is it default or should we implement externally.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
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

Joined: Jun 07, 2008
Posts: 16
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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
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

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Struts ThreadSafe