Thread safety in static methods

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.
 
B

Brian Gideon

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.
 
A

aesper

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top