开发者

concurrent access to static methods in java, is synchronization needed?

Do different threads accessing method "foo" have their own copy of local variables, or it is needed to make this method synchronized?

class X {
   static returnType foo( Object arg) {
      Obje开发者_如何学JAVAct localvar;
      // perform some calculation based on localvar and arg.
      // no non-local variable is used i.e: this is a utility method.
      // return something.
   }
}


You don't need to synchronize that method. The local variable gets created in the current thread's "memory space" and there is no way that it will get accessed by any other thread (from what you've shown above).


Since the variables used are defined/used in it's own scope there is no need for syncronize the method.


The method should not be synchronized but you should use a final variable arg ie
static returnType foo(final Object arg).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜