是“不稳定”的吗?在这种情况下需要关键字吗? (爪哇)

发布于 2024-12-04 22:17:58 字数 577 浏览 0 评论 0原文

我有以下代码,它被初始化为类中的静态变量:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独陪着我 2024-12-11 22:17:58

是否保证从 connect() 生成的线程
方法会看到“connected”不再等于“true”?

仅当从 connect() 方法生成的线程是将 connected 设置为 false 时!

生成的线程在启动后就能看到connected为true,因为启动线程是一个happens-before动作,源代码中还建立了一个 >发生在线程内排序之前

但是,如果父线程在生成的线程上调用 start() 之后清除了 connected 标志,则无法保证生成的线程 才能看到更改,除非您将标志声明为易失性

1.4 和 1.5 行为之间的主要区别在于,从 Java 5 开始,写入易失性变量也会刷新对非易失性变量的写入。 (读取易失性变量也会清除所有缓存的非易失性变量值。)由于您似乎只涉及一个变量,因此这一更改不会影响您。

Is it guaranteed that the thread that was spawned from the connect()
method will see that "connected" is no longer equal to "true"?

Only if the thread that was spawned from the connect() method is the one that sets connected to false!

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 calling start() on the spawned thread, the spawned thread is not guaranteed to see the change unless you declare the flag as 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. (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.

毅然前行 2024-12-11 22:17:58

是的,你应该使用易失性。这将确保当字段的值更新时,检查该字段的所有线程都将具有更新的值。否则,您无法确保不同的线程将获得更新的值。

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.

☆獨立☆ 2024-12-11 22:17:58

好吧,即使你不添加 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文