Are method parameters thread safe in Java?
Asked Answered
I

6

15
Class Shared{    
     public void sharedMethod(Object o){
          //does something to Object
     }     
}

//this is how threads call the shared method
run(){
     sharedInstance.sharedMethod(someObject);
}

Now the o is being passed as the parameter to the method. And the same method is being called by multiple threads in parallel. Can we safely say that this code is thread safe?

There are two scenarios:

  • If the someObject is being shared among the threads
  • If every Thread has its own copy of someObject
Investiture answered 31/8, 2013 at 11:28 Comment(1)
No you can't say that.Evenings
G
17

No you cannot say that. Method parameters are local to threads, meaning each one has their own copy of the o reference variable, But if you call this method with the same object from multiple threads, then the argument will be shared between them (remember that Java is pass-by-value). In that case, you need to provide explicit synchronization to avoid troubles.

Gyrostatic answered 31/8, 2013 at 11:30 Comment(1)
You have 2 distinct copies of the "reference" (one in calling method, one in called method - because of pass-by-value), not the "object" itself. Changing the "reference" in one method does not affect the other (for example o=null in called method does not nullify the o in calling method), but changing the "object" that they refer to (in this case both refer to the same object) is visible to the other. Hence both threads work on the same object and hence not thread safe.Freewheel
S
4

Yes, but only in two scenarios:

  • if every object you pass in the o parameter is immutable,
  • if your code guarantees that there is at most one thread working on the object referenced by o.

Otherwise - no, since the internal state of the object can be changed by multiple threads concurrently.

Shellashellac answered 31/8, 2013 at 11:33 Comment(0)
D
2

When you create a thread it will have its own stack created (method and local variables).

Two threads will have two stacks and one thread never shares its stack with any other thread.

It will not effect until you are calling this on same object.

Dammar answered 31/8, 2013 at 11:38 Comment(1)
@Narenda is talking about object parameters (pass by reference), so it's not thread safe.Faceoff
E
1

If you call the same method in multiple threads, and pass it the same object, that object is absolutely not safe.

Evenings answered 31/8, 2013 at 11:33 Comment(0)
Z
0

Local variables are stored in each thread's own stack. That means that local variables are never shared between threads. That also means that all local primitive variables are thread safe.

EDIT

The LocalObject instance in this example is not returned from the method, nor is it passed to any other objects that are accessible from outside the sharedMethod() method.

Each thread executing the sharedMethod() method will make use of its own Object o as parameter.

So, the whole method sharedMethod() seems to be thread safe.

The only exception is of course, if one of the methods called with the Object o as parameter, stores the Object o instance in a way that allows access to it from other threads.

Zoomorphism answered 31/8, 2013 at 11:49 Comment(0)
F
0

Treat it this way.

If your threads don't share any common resources, than it's impossible to have concurrency problems. As much as we can tell from the information you provided, the only thing that can be shared here, is someObject. If it's thread-safe itself, or being copied for each thread, than your code is thread safe in general, unless there are other shared resources.

Fastback answered 31/8, 2013 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.