• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is passing an object to a static method threadsafe?  RSS feed

 
Kyle Bentley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a domain object that I pass to a static method:



My question here is does passing an object to a static method (that does modify the incoming object) a truly thread safe operation?

Thanks for your thoughts on this.

-Kyle
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking whether the act of passing the reference to the method is thread-safe, or are you asking for a blanket assurance that anything the method does with the object is thread-safe?

The answer to the second question is "Not necessarily" and the fact that the method is static is irrelevant to the issue of thread safety.

I believe the answer to the first question is "Yes", but again the fact that the method is static is irrelevant.
 
Kyle Bentley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Are you asking whether the act of passing the reference to the method is thread-safe, or are you asking for a blanket assurance that anything the method does with the object is thread-safe?

The answer to the second question is "Not necessarily" and the fact that the method is static is irrelevant to the issue of thread safety.

I believe the answer to the first question is "Yes", but again the fact that the method is static is irrelevant.


I am asking whether anything intrinsic to static method can make something not thread safe. From what I gather from your response, the answer is 'no'.

Obviously, a static method attempting to modify another static element or some other shared variable would present itself with conflicts around thread safety, but in a case where the static method is only accessing and changing a local object (via reference) created within the same thread would seem to indicate that there is no problem with concurrency from what you are saying.

 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Bentley wrote:
Obviously, a static method attempting to modify another static element or some other shared variable would present itself with conflicts around thread safety, but in a case where the static method is only accessing and changing a local object (via reference) created within the same thread would seem to indicate that there is no problem with concurrency from what you are saying.


The rule is simple. If code modifies data shared among threads, then this code is not threadsafe.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!