是“不稳定”的吗?在这种情况下需要关键字吗? (爪哇)
我有以下代码,它被初始化为类中的静态变量:
public class MyXlet extends Xlet {
boolean connected = false;
...
void connect() {
// some code goes here, starts a new thread
MyXlet.connected = true;
}
void disconnect() {
// some code goes here, the new thread is designed to terminate once connected is false;
MyXlet.connected = false;
}
}
假设我已经运行了 connect 方法,该方法生成了一个新线程。 connect() 方法将“connected”设置为“false”。是否保证从 connect() 方法生成的线程将看到“connected”不再等于“true”?或者我必须为此使用“connected”上的 volatile 关键字吗?值得注意的是,我使用的是 Java 1.4.2。
I have the following code that gets initialized as a static variable in a class:
public class MyXlet extends Xlet {
boolean connected = false;
...
void connect() {
// some code goes here, starts a new thread
MyXlet.connected = true;
}
void disconnect() {
// some code goes here, the new thread is designed to terminate once connected is false;
MyXlet.connected = false;
}
}
Let's say I have already run the connect method, which spawns a new thread. The disconnect() method sets "connected" to "false". Is it guaranteed that the thread that was spawned from the connect() method will see that "connected" is no longer equal to "true"? Or will I have to use the volatile keyword on "connected" for this? It is worthy to note that I am using Java 1.4.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当从
connect()
方法生成的线程是将connected
设置为false
时!生成的线程在启动后就能看到
connected
为true,因为启动线程是一个happens-before动作,源代码中还建立了一个 >发生在线程内排序之前。但是,如果父线程在生成的线程上调用
start()
之后清除了connected
标志,则无法保证生成的线程 才能看到更改,除非您将标志声明为易失性
。1.4 和 1.5 行为之间的主要区别在于,从 Java 5 开始,写入易失性变量也会刷新对非易失性变量的写入。 (读取易失性变量也会清除所有缓存的非易失性变量值。)由于您似乎只涉及一个变量,因此这一更改不会影响您。
Only if the thread that was spawned from the
connect()
method is the one that setsconnected
tofalse
!The spawned thread will be able to see that
connected
is true after it is started, because starting a thread is a happens-before action, and source code also establishes a happens-before ordering within a thread.But, if the parent thread clears the
connected
flag after callingstart()
on the spawned thread, the spawned thread is not guaranteed to see the change unless you declare the flag asvolatile
.The main difference between 1.4 and 1.5 behavior is that writing to a
volatile
variable will also flush writes to non-volatile variables from Java 5 onward. (Reading a volatile variable also clears any cached non-volatile variable values.) Since it appears that you only have one variable involved, this change shouldn't affect you.是的,你应该使用易失性。这将确保当字段的值更新时,检查该字段的所有线程都将具有更新的值。否则,您无法确保不同的线程将获得更新的值。
Yes you should use volatile. This will ensure that when the field's value is updated all threads that check the field will have the updated value. Otherwise you are not ensured that different threads will get the updated value.
好吧,即使你不添加 volatile 关键字,其他线程也将能够读取连接的变量。通过添加 volatile 等,您可以同步访问变量。
Well, even if you don't add the volatile keyword, other threads will be able to read the connected variable. By adding volatile, amongst other things, you make access to the variable synchronous.