简体   繁体   English

在这种情况下是否需要“易失性”关键字词? (Java)

[英]Is the “volatile” keyword word needed in this instance? (Java)

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. 假设我已经运行了connect方法,它将产生一个新线程。 The disconnect() method sets "connected" to "false". Disconnect()方法将“ connected”设置为“ false”。 Is it guaranteed that the thread that was spawned from the connect() method will see that "connected" is no longer equal to "true"? 是否可以保证从connect()方法产生的线程将看到“ connected”不再等于“ true”? Or will I have to use the volatile keyword on "connected" for this? 还是我必须在“已连接”上使用volatile关键字? It is worthy to note that I am using Java 1.4.2. 值得注意的是,我正在使用Java 1.4.2。

Is it guaranteed that the thread that was spawned from the connect() method will see that "connected" is no longer equal to "true"? 是否可以保证从connect()方法产生的线程将看到“ connected”不再等于“ true”?

Only if the thread that was spawned from the connect() method is the one that sets connected to false ! 只有当这是从催生了螺纹connect()方法是设定一个connectedfalse

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. 生成的线程在启动后将能够看到connected为真,因为启动线程是“ 先发生”操作,并且源代码也会在线程内进行排序之前建立“ 先发生”操作

But, if the parent thread clears the connected flag after calling start() on the spawned thread, the spawned thread is not guaranteed to see the change unless you declare the flag as volatile . 但是,如果父线程在生成的线程上调用start() 之后清除connected标志,则除非保证将标志声明为volatile否则不能保证生成的线程会看到更改。

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. 1.4和1.5行为之间的主要区别在于,写入volatile变量还将刷新从Java 5开始对非易失性变量的写入。 (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. 是的,您应该使用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. 否则,您将无法确保不同的线程将获得更新后的值。

Well, even if you don't add the volatile keyword, other threads will be able to read the connected variable. 好吧,即使您没有添加volatile关键字,其他线程也将能够读取连接的变量。 By adding volatile, amongst other things, you make access to the variable synchronous. 通过添加volatile等,您可以同步访问变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM