• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

am I thread safe with this dispachAction?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's what I had in mind.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Merrill
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic