Java Lock支持内存一致性

发布于 2024-08-25 16:57:40 字数 172 浏览 11 评论 0 原文

Java 6 API 问题。调用 LockSupport.unpark(thread) 与刚刚取消停放的线程中的 LockSupport.park 返回是否存在 happens-before 关系?我强烈怀疑答案是肯定的,但 Javadoc 似乎没有明确提及。

Java 6 API question. Does calling LockSupport.unpark(thread) have a happens-before relationship to the return from LockSupport.park in the just-unparked thread? I strongly suspect the answer is yes, but the Javadoc doesn't seem to mention it explicitly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

似最初 2024-09-01 16:57:40

我刚刚发现这个问题是因为我也在问自己同样的事情。根据 Oracle 研究员 这篇文章 .com/pls/apex/f?p=labs%3abio:0:29" rel="noreferrer">David Dice,答案似乎是。这是文章的相关部分:

如果一个线程在 park() 中被阻塞,我们保证后续的线程会被阻塞。
unpark() 将使其准备就绪。完全合法但质量低劣
park()unpark() 的实现将是空方法,其中
程序退化为简单的旋转。 事实上这就是
试金石测试 park()-unpark() 用法是否正确。

清空 park()unpark()< /code> 方法不会给您任何 happens-before 关系保证,因此为了使您的程序 100% 可移植,您不应该依赖它们。

再说一次,LockSupport 的 Javadoc 说:

这些方法旨在用作创建
更高级别的同步实用程序,并且本身并不存在
对于大多数并发控制应用程序很有用。 park 方法是
设计仅用于以下形式的结构:

while (!canProceed()) { ... LockSupport.park(this); }

park() 的弱保证实际上不应该是有问题吧?

I have just found this question because I was asking myself the same thing. According to this article by Oracle researcher David Dice, the answer seems to be no. Here's the relevant part of the article:

If a thread is blocked in park() we're guaranteed that a subsequent
unpark() will make it ready. A perfectly legal but low-quality
implementation of park() and unpark() would be empty methods, in which
the program degenerates to simple spinning. An in fact that's the
litmus test for correct park()-unpark() usage.

Empty park() and unpark() methods do not give you any happens-before relationship guarantees, so for your program to be 100% portable, you should not rely on them.

Then again, the Javadoc of LockSupport says:

These methods are designed to be used as tools for creating
higher-level synchronization utilities, and are not in themselves
useful for most concurrency control applications. The park method is
designed for use only in constructions of the form:

while (!canProceed()) { ... LockSupport.park(this); }

Since you have to explicitly check some condition anyway, which will either involve volatile or properly synchronized variables, the weak guarantees of park() should not actually be problem, right?

绅刃 2024-09-01 16:57:40

如果没有这样记录,那么您不能依赖它来创建发生在关系之前的关系。

具体来说,Hotspot 代码中的 LockSupport.java 只是调用 Unsafe.park 和 .unpark!

发生之前关系通常来自易失性状态标志或类似标志上的写-读对。

请记住,如果它没有被记录为创建发生之前关系,那么您必须将其视为没有创建,即使您可以证明它在您的特定情况下确实如此系统。未来的系统和实施可能不会。他们给自己留下这种自由是有充分理由的。

If it isn't documented as such then you CANNOT rely on it creating a happens before relationship.

Specifically LockSupport.java in Hotspot code simply calls Unsafe.park and .unpark!

The happens-before relationship will generally come from a write-read pair on a volatile status flag or something similar.

Remember, if it isn't documented as creating a happens-before relationship then you must treat it as though it does not even if you can prove that it does on your specific system. Future systems and implementations may not. They left themselves that freedom for good reason.

唐婉 2024-09-01 16:57:40

我查看了 JDK 代码,看起来 LockSupport 方法通常是在同步块之外调用的。所以,你的假设似乎是正确的。

I have looked though the JDK code and it looks like LockSupport methods are normally called outside of synchronization blocks. So, your assumption seems to be correct.

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