java 静态方法和线程安全中的局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11535237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:29:01  来源:igfitidea点击:

Local variables in static method and thread safety

javastaticthread-safety

提问by Daehee Han

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 Afor each thread.

现在我创建两个线程,并A为每个线程创建一个实例。

  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?

  1. 当我调用test()每个线程时,我能保证它test()是线程安全的吗?

  2. 本地变量test()存储在哪里?每个线程的堆栈?堆空间?

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!

PS我知道在这种情况下静态是完全没有意义的。我在我们的遗留代码中找到了它;我只是想确定我所知道的!

回答by Erol

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.

局部变量存储在每个线程自己的堆栈中。这意味着局部变量永远不会在线程之间共享。这也意味着所有本地原始变量都是线程安全的。

Local references to objects are a bit different. The reference itself is not shared. The object referenced however, is not stored in each threads's local stack. All objects are stored in the shared heap. If an object created locally never escapes the method it was created in, it is thread safe. In fact you can also pass it on to other methods and objects as long as none of these methods or objects make the passed object available to other threads.

对对象的局部引用有点不同。引用本身不共享。然而,引用的对象并未存储在每个线程的本地堆栈中。所有对象都存储在共享堆中。如果本地创建的对象从不转义其创建方法,则它是线程安全的。事实上,您也可以将它传递给其他方法和对象,只要这些方法或对象都不使传递的对象可用于其他线程。

Object members are stored on the heap along with the object. Therefore, if two threads call a method on the same object instance and this method updates object members, the method is not thread safe.

对象成员与对象一起存储在堆中。因此,如果两个线程在同一个对象实例上调用一个方法并且此方法更新对象成员,则该方法不是线程安全的。

Thread safety check: If a resource is created, used and disposed within the control of the same thread, and never escapes the control of this thread,the use of that resource is thread safe.

线程安全检查:如果一个资源是在同一个线程的控制下创建、使用和处理的,并且永远不会脱离该线程的控制,那么该资源的使用是线程安全的。

From: http://tutorials.jenkov.com/java-concurrency/thread-safety.html

来自:http: //tutorials.jenkov.com/java-concurrency/thread-safety.html

回答by Pramod Kumar

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

当我在每个线程中调用 test() 时,我能保证 test() 是线程安全的吗?

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

是的,如果在 test() 方法中您正在处理方法局部变量,那将是线程安全的。

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

test() 中的局部变量存储在哪里?每个线程的堆栈?堆空间?

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

方法局部变量存储每个线程自己的堆栈。

回答by nhahtdh

For number 1, I don't know what test()does, so I cannot answer. If they modify some staticvariable 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.

对于第 1 点,我不知道是什么test(),所以我无法回答。如果他们修改了staticA 类的某个变量,那么它可能不是线程安全的。如果沿途的两个线程都引用了同一个对象,则取决于对象的定义方式,它可能不是线程安全的。

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.

对于数字 2,局部变量位于每个线程的堆栈中(或至少在概念上是这样),因此不必担心其他线程会修改局部变量。