aspose file tools*
The moose likes Struts and the fly likes question about action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "question about action class" Watch "question about action class" New topic
Author

question about action class

Tina Ma
Ranch Hand

Joined: Nov 29, 2007
Posts: 198
Is an action class thread safe by itself?


Tina
SCJP 1.4, SCWCD 1.4
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Tina Ma
Ranch Hand

Joined: Nov 29, 2007
Posts: 198
thanks Lee. The "thread safety" has always been a little confusing issue for me.
abhishek paul
Greenhorn

Joined: Sep 26, 2006
Posts: 25
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.


SCJP5 -
To repeat what others have said, requires education, to challenge it , requires brains.
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 460
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.


Visit my blog: http://jnassef.blogspot.com/
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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

Joined: Jan 28, 2008
Posts: 460
So to sum it up, struts 1 is not thread safe. Struts 2 is thread safe
 
wood burning stoves
 
subject: question about action class