2

If I have one instance of an object A and it has an instance method foo() with only variables created and used in that method is that method thread safe even if the same instance is accessed by many threads?

If yes, does this still apply if instance method bar() on object A creates many threads and calls method foo() in the text described above?

Does it mean that every thread gets a "copy" of the method even if it belongs to the same instance?

I have deliberately not used the synchronized keyword.

Thanks

user1329339
  • 1,295
  • 1
  • 11
  • 26
  • Based in what you describe I have no idea. If the local variables are initialized by methods that are not themselves thread safe, then `foo()` is not as well. Do you have some code you can share? – jdphenix Aug 13 '14 at 15:13

3 Answers3

8

Yes. All local variables (variables defined within a method) will be on their own Stack frame. So, they will be thread safe, provided a reference is not escaping the scope (method)

Note : If a local reference escapes the method (as an argument to another method) or a method works on some class level or instance level fields, then it is not thread-safe.

Does it mean that every thread gets a "copy" of the method even if it belongs to the same instance

No, there will be only one method. every thread shares the same method. But each Thread will have its own Stack Frame and local variables will be on that thread's Stack frame. Even if you use synchronize on local Objects, Escape Analysis proves that the JVM will optimize your code and remove all kinds of synchronization.

example :

public static void main(String[] args) {

    Object lock = new Object();
    synchronized (lock) {
        System.out.println("hello");
    }
}

will be effectively converted to :

public static void main(String[] args) {

        Object lock = new Object(); // JVm might as well remove this line as unused Object or instantiate this on the stack
        System.out.println("hello");

}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 1
    Ok, thanks for the quick response! Can you please share a link to documentation regarding this particular issue ? I can't see any mention of methods having their own stack frame in Oracle's usual java thread tutorial. – user1329339 Aug 13 '14 at 15:16
  • @user1329339 - here you go - http://stackoverflow.com/questions/12825847/why-are-local-variables-thread-safe-in-java – TheLostMind Aug 13 '14 at 15:17
  • @user1329339 - stack frame is explained here - http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html . It is not part of Threads* tutorial. Its part of the JVM spec. – TheLostMind Aug 13 '14 at 15:23
2

You have to separate the code being run, and the data being worked on.

The method is code, executed by each of the threads. If that code contains a statement such as int i=5 which defines a new variable i, and sets its value to 5, then each thread will create that variable.

The problem with multi-threading is not with common code, but with common data (and other common resources). If the common code accesses some variable j that was created elsewhere, then all threads will access the same variable j, i.e. the same data. If one of these threads modifies the shared data while the others are reading, all kinds of errors might occur.

Now, regarding your question, your code should be thread safe as long as your variables are defined within bar(), and bar() doesn't access some common resource such as a file.

Malt
  • 28,965
  • 9
  • 65
  • 105
0

You should post some example code to make sure we understand the use case.

For this example:

public class Test {
    private String varA;

    public void doSomething() {
        String varB;

    }
}

If you don't do anything to modify varA in this example and only modify varB, this example is Thread Safe.

If, however, you create or modify varA and depend on it's state, then the method is NOT Thread Safe.

Baldy
  • 2,002
  • 14
  • 14