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

时间:2022-01-02 03:13:36

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.

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

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

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

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

    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!

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

3 个解决方案

#1


30  

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

#2


3  

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.

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

#3


0  

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.

对于数字1,我​​不知道test()的作用,所以我无法回答。如果他们修改了A类的某些静态变量,那么它可能不是线程安全的。如果沿途的两个线程都被赋予对同一对象的引用,则根据对象的定义方式,它可能不是线程安全的。

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,局部变量位于每个线程的堆栈中(或者至少在概念上类似),因此不必担心其他线程正在修改局部变量。

#1


30  

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

#2


3  

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.

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

#3


0  

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.

对于数字1,我​​不知道test()的作用,所以我无法回答。如果他们修改了A类的某些静态变量,那么它可能不是线程安全的。如果沿途的两个线程都被赋予对同一对象的引用,则根据对象的定义方式,它可能不是线程安全的。

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,局部变量位于每个线程的堆栈中(或者至少在概念上类似),因此不必担心其他线程正在修改局部变量。