Thread safety in static methods

  • Thread starter Thread starter aesper
  • Start date Start date
A

aesper

I have a question about using statics in a multithreaded application.
I know that when threads share resources we need to be carefull and make
sure they are well synchronized when accessing these resources. My question
is about static methods that only deal with mehod-local variables. Do we
need to do any special locking if such method can be called from different
threads simultaneously?
Does the locking of static methods happen automatically?
Any help is appreciated.
 
I have a question about using statics in a multithreaded application.
I know that when threads share resources we need to be carefull and make
sure they are well synchronized when accessing these resources. My question
is about static methods that only deal with mehod-local variables. Do we
need to do any special locking if such method can be called from different
threads simultaneously?
Does the locking of static methods happen automatically?
Any help is appreciated.

It does not happen automatically. If the method uses nothing more
than local variables and parameters then it should be safe. That's
assuming that the objects referenced by the parameter variables are
not shared between threads. Of course, if that were the case then it
would be the caller's responsiblity to use the appropriate
synchronization mechanisms and not the method's anyway.
 
Thanks for the response.

Brian Gideon said:
It does not happen automatically. If the method uses nothing more
than local variables and parameters then it should be safe. That's
assuming that the objects referenced by the parameter variables are
not shared between threads. Of course, if that were the case then it
would be the caller's responsiblity to use the appropriate
synchronization mechanisms and not the method's anyway.
 
Back
Top