java 多线程中的静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17500259/
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
static variables in multithreading
提问by R9J
I found that declaring a variable as static makes no sense
in Multi-Threading. I assume that, this is because of every thread has its own stack
. Is this the only reason?
I found that declaring a variable as static makes no sense
在多线程中。我认为,这是因为every thread has its own stack
. 这是唯一的原因吗?
I know that static variables should be used within synchronized block
. but why?
我知道static variables should be used within synchronized block
。但为什么?
回答by Juned Ahsan
static makes no sense in Multi-Threading.
静态在多线程中没有意义。
Im afraid you are making the reverse statement. Static variable is a shared resource, which can be used to exchange some information among different threads. And we need to be careful while accessing such a shared resource. Hence, we need to make sure that the access to static variables in multi-threaded environment is synchronized.
恐怕你说反了。静态变量是一种共享资源,可以用来在不同线程之间交换一些信息。我们在访问这样的共享资源时需要小心。因此,我们需要确保在多线程环境中对静态变量的访问是同步的。
every thread has its own stack
每个线程都有自己的堆栈
This is a correct statement. Each thread has its own stack but they share the process heap.
Stack holds only the local variables and not the variables on the heap. Static variables are stored in the PermGen
section of the heap and hence the access to them should be well guarded.
这是一个正确的说法。Each thread has its own stack but they share the process heap.
堆栈只保存局部变量,而不保存堆上的变量。静态变量存储在PermGen
堆的部分中,因此应该很好地保护对它们的访问。
回答by Divers
Because first part of question is already answered, I will try to answer on second question.
因为问题的第一部分已经回答了,我将尝试回答第二个问题。
I know that static variables should be used within synchronized block. but why?
我知道应该在同步块中使用静态变量。但为什么?
Because if you don't use atomic, operations with variables are not atomic. That's why you should block variables while working with them. But in real world, you can use volatilekeyword, that will guarantee you, that threads will have actual values of variable.
因为如果您不使用atomic,则带有变量的操作就不是原子的。这就是为什么你应该在使用变量时阻止它们。但在现实世界中,您可以使用volatile关键字,这将向您保证,线程将具有变量的实际值。
回答by Devolus
If you change a variable in a multithreaded environment, the new value may not neccessarily visibile as it might be cached. This is also true for static variables of course. If you don't use a synchronized block you might consider using volatile
instead. This will also guaruantee that the various threads get an updated copy, without the need of synchronizing.
Wether volatile
is enough four your application depends on your requirements.
如果您在多线程环境中更改变量,则新值可能不一定可见,因为它可能已被缓存。当然,对于静态变量也是如此。如果您不使用同步块,您可以考虑volatile
改用。这也将保证各个线程获得更新的副本,而无需同步。阉volatile
足够Four的应用程序依赖于您的需求。
回答by Alexander Kulyakhtin
Add volatile
to your static declaration.
添加volatile
到您的静态声明。
volatile
will guarantee any other thread will see the most recent value of the variable. So, with volatile it will make sense.
volatile
将保证任何其他线程将看到该变量的最新值。因此,使用 volatile 将是有道理的。
However, volatile
will not guarantee atomicity. If you write to your variable from more than one thread you might want to use atomics or synchronize
block.
但是,volatile
不会保证原子性。如果您从多个线程写入变量,您可能需要使用原子或synchronize
块。
I think volatile
will be fine.
我想volatile
会好的。