| Author |
am I thread safe with this dispachAction?
|
Peter Primrose
Ranch Hand
Joined: Sep 10, 2004
Posts: 755
|
|
just to be on the safe side: thread question i have a dispatchAction and there are 4 methods methodA methodB methodC methodD say that methodD does the same as methodA + extra. I wonder if it is possible to do this (code below) and not get errors in runtime while other users are using the application (meaning thread-wise to be safe) thanks you! [ February 20, 2007: Message edited by: Bear Bibeault ]
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
I don't see any issues with thread safety in this approach. However, I don't believe this is the best approach. A method called by Struts is meant to do something and then return a forward that tells Struts what to do next. With this approach, if Method A finds an error and wants to redirect to a different forward, Method D is unaware of it. The approach I would suggest would be to refactor and put the logic that Method A and Method D have in common into a new private method. Then have both Method A and Method D call the same method to do that portion of the work and then return the appropriate forward.
|
Merrill
Consultant, Sima Solutions
|
 |
Peter Primrose
Ranch Hand
Joined: Sep 10, 2004
Posts: 755
|
|
so if I understand you, you're recommending to do this: private void myMethod() { } public ActionForward methodA(...) { myMethod(); } public ActionForward methodB(...) { myMethod(); . . . } I read in "The thread framework" that it is not recommended to share variable between methods (maybe i took it to the extreme) ubt I just wanted to be sure. thanks
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
|
Yes, that's what I had in mind.
|
 |
Peter Primrose
Ranch Hand
Joined: Sep 10, 2004
Posts: 755
|
|
|
thank you Merrill
|
 |
Brent Sterling
Ranch Hand
Joined: Feb 08, 2006
Posts: 948
|
|
As far as thread safety goes....there are no issues with passing values as parameters to methods. Parameters are stored on the stack and every thread has its own stack. The biggest issue is with using class member variables because there is only one instance of the Action class so all users would be using the same member variable. You would NOT want to do something like the following. The issue being that by the time that myMethod accesses the member variable userId another thread may have executed and changed the value. - Brent
|
 |
 |
|
|
subject: am I thread safe with this dispachAction?
|
|
|