Thread safety in static methods
am 20.12.2007 18:11:00 von 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.
Re: Thread safety in static methods
am 20.12.2007 19:32:21 von Brian Gideon
On Dec 20, 11:11 am, aesper wrote:
> 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.
Re: Thread safety in static methods
am 20.12.2007 22:07:01 von aesper
Thanks for the response.
"Brian Gideon" wrote:
> On Dec 20, 11:11 am, aesper wrote:
> > 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.
>