Local variables in static method and thread safety

前端 未结 3 1456
悲哀的现实
悲哀的现实 2021-02-01 05:32

I have a question about variable scope.

For example:

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


        
3条回答
  •  逝去的感伤
    2021-02-01 06:09

    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.

提交回复
热议问题