File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about action class

 
Tina Ma
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is an action class thread safe by itself?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Struts 1, yes, only a single instance of the action class is created.

For Struts 2, no, but an instance is created for each request and is not shared and it is discarded after use.

For more information, please refer to this.
 
Tina Ma
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Lee. The "thread safety" has always been a little confusing issue for me.
 
abhishek paul
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..although you seem satisfied by the explaination heres some interesting read on the thread safety issue of Action classes... http://www.mail-archive.com/struts-user@jakarta.apache.org/msg73540.html

Regards,
Abhishek.
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chengwei Lee:
For Struts 1, yes, only a single instance of the action class is created.

For Struts 2, no, but an instance is created for each request and is not shared and it is discarded after use.

For more information, please refer to this.


Again I disagree. Actions in struts 1 are not thread safe by itself. You have to make sure of that yourself. This means that either you don't use instance variables, use thread safe instance variables only, or make sure that no two threads would access the same instance variable at the same time using synchronization. You could of course override the request processor, or use spring and make the scope of the action beans prototype instead of singleton. But actions by default (in struts 1) are not thread safe.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alaa Nassef:


Again I disagree. Actions in struts 1 are not thread safe by itself. You have to make sure of that yourself. This means that either you don't use instance variables, use thread safe instance variables only, or make sure that no two threads would access the same instance variable at the same time using synchronization. You could of course override the request processor, or use spring and make the scope of the action beans prototype instead of singleton. But actions by default (in struts 1) are not thread safe.


You're right in that you need to make it thread-safe, it doesn't happen out of the box, but arguably, it is still thread-safe (after making it so).

Whereas in Struts 2, developers need not worry about it being not thread-safe.
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to sum it up, struts 1 is not thread safe. Struts 2 is thread safe
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic