本文介绍了Java多线程&安全出版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读《Java并发实践》后和实践中的OSGI"我发现一个特定的主题非常有趣;安全出版.以下来自JCIP:

After reading "Java concurrent in practice" and "OSGI in practice" I found a specific subject very interesting; Safe Publication. The following is from JCIP:

要安全地发布对象,必须同时使对对象的引用和对象的状态对其他线程可见.正确构造的对象可以通过以下方式安全地发布:

  • 从一个静态初始化器初始化一个对象引用.
  • 将对其的引用存储到 volatile 字段中.
  • 将对其的引用存储到 final 字段中.
  • 将对其的引用存储到由(同步)锁适当保护的字段中.
  • Initializing an object reference from a static initializer.
  • Storing a reference to it into a volatile field.
  • Storing a reference to it into a final field.
  • Storing a reference to it into a field that is properly guarded by a (synchronized) lock.

我的第一个问题:有多少 Java 开发人员意识到这个(问题)?有多少现实世界的 Java 应用程序真正遵循这一点,这真的是一个真正的问题吗?我有一种感觉,99% 的已实现 JVM 并没有那么邪恶",即不能保证线程(实际上它实际上(几乎)不可能")看到陈旧数据只是因为引用不是遵循安全出版习惯"

My first question: how many java developers are aware of this (problem)?How many real world java applications are really following this, AND is this really a real problem? I have a feeling that 99% of the implemented JVMs out there are not that "evil", i.e. a thread is not guaranteed (in fact its practical (almost) "impossible") to see stale data just because the reference is not following the "safe publication idiom" above.

推荐答案

按比例来说,可以说很少有程序员充分了解同步和并发.谁知道现在有多少服务器应用程序管理着金融交易、医疗记录、警察记录、电话等,这些应用程序充满了同步错误并且基本上是意外工作,或者非常非常偶尔失败(从未听说有人得到幻影电话费被添加到他们的电话帐单中?)出于从未真正调查或深入了解的原因.

Proportionally, it's probably fair to say that very few programmers sufficiently understand synchronization and concurrency. Who knows how many server applications there are out there right now managing financial transactions, medical records, police records, telephony etc etc that are full of synchronization bugs and essentially work by accident, or very very occasionally fail (never heard of anybody get a phantom phone call added to their telephone bill?) for reasons that are never really looked into or gotten to the bottom of.

对象发布是一个特殊的问题,因为它经常被忽视,而且在这个地方编译器进行优化可能会导致意外行为,如果你不知道它是非常合理的:在 JIT 编译的代码中,存储一个指针,然后增加它并存储数据是一件非常合理的事情.您可能认为它是邪恶的",但在低级别上,这确实是您期望的 JVM 规范.(顺便说一句,我听说过在 JRockit 中运行的实际程序遇到了这个问题——这不是纯粹的理论问题.)

Object publication is a particular problem because it's often overlooked, and it's a place where it's quite reasonable for compilers to make optimisations that could result in unexpected behaviour if you don't know about it: in the JIT-compiled code, storing a pointer, then incrementing it and storing the data is a very reasonable thing to do. You might think it's "evil", but at a low level, it's really how you'd expect the JVM spec to be. (Incidentally, I've heard of real-life programs running in JRockit suffering from this problem-- it's not purely theoretical.)

如果您知道您的应用程序存在同步错误,但在当前硬件上的当前 JVM 中没有出现错误行为,那么 (a) 恭喜;(b),现在是时候开始冷静地走向消防出口",在需要升级太多组件之前修复代码并教育程序员.

If you know that your application has synchronization bugs but isn't misbehaving in your current JVM on your current hardware, then (a) congratulations; and (b), now is the time to start "walking calmly towards the fire exit", fixing your code and educating your programmers before you need to upgrade too many components.

这篇关于Java多线程&安全出版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:11