aspose file tools
The moose likes Java in General and the fly likes Is passing an object to a static method threadsafe? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is passing an object to a static method threadsafe?" Watch "Is passing an object to a static method threadsafe?" New topic
Author

Is passing an object to a static method threadsafe?

Kyle Bentley
Greenhorn

Joined: Nov 09, 2009
Posts: 2

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
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Nov 09, 2009
Posts: 2
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

Joined: Oct 22, 2009
Posts: 237
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Is passing an object to a static method threadsafe?
 
Similar Threads
Garbage Collection Problem Explain Pls Dan Chisholm single topic
Referenec passing vs. Valuse passing
My search method is not working ( vectors)
Passing values from one method to another
Passing a String reference to a method.