'Local variables in static method and thread safety

I have a question about variable scope.

For example:

class A {
    private static void test() {
        // do something with local variables
    }
}

Now I make two threads, and create one instance of A for each thread.

  1. When I call test() in each thread, can I guarantee that test() is thread safe?

  2. Where are the local varibles in test() stored? Each threads' stack? Heap space?

P.S. I know that static is totally pointless in this case. I found it in our legacy code; I just wanna make sure what I know!



Solution 1:[1]

When I call test() in each thread, can I guarantee that test() is thread safe?

Yes it would be thread safe if in test() method you are working on method local variables.

Where are the local varibles in test() stored? each threads' stack? heap space?

Method Local variable are stored each thread's own stack.

Solution 2:[2]

For number 1, I don't know what test() does, so I cannot answer. If they modify some static variable of the class A, then it may not be thread safe. If both threads along the way are given reference to the same object, depending on how the object is defined, it might not be thread safe.

For number 2, local variables are in the stack of each thread (or at least conceptually like that), so there is no worry about the local variables being modified by the other threads.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 nhahtdh