Java 比较和交换语义和性能 [英] Java compare and swap semantics and performance

查看:29
本文介绍了Java 比较和交换语义和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中比较和交换的语义是什么?即,AtomicInteger 的比较和交换方法是否只是保证不同线程之间对原子整数实例的特定内存位置的有序访问,还是保证对内存中所有位置的有序访问,即它就像一个 volatile(一个内存栅栏).

What is the semantics of compare and swap in Java? Namely, does the compare and swap method of an AtomicInteger just guarantee ordered access between different threads to the particular memory location of the atomic integer instance, or does it guarantee ordered access to all the locations in memory, i.e. it acts as if it were a volatile (a memory fence).

来自文档:

  • weakCompareAndSet 以原子方式读取和有条件地写入变量,但不创建任何发生在排序之前,因此不提供对先前或后续读取和写入除目标对象之外的任何变量的保证weakCompareAndSet.
  • compareAndSet 和所有其他读取和更新操作(例如 getAndIncrement)都具有读取和写入易失性变量的记忆效应.
  • weakCompareAndSet atomically reads and conditionally writes a variable but does not create any happens-before orderings, so provides no guarantees with respect to previous or subsequent reads and writes of any variables other than the target of the weakCompareAndSet.
  • compareAndSet and all other read-and-update operations such as getAndIncrement have the memory effects of both reading and writing volatile variables.

从 API 文档中可以明显看出 compareAndSet 就像一个易失性变量一样.然而,weakCompareAndSet 应该只是改变其特定的内存位置.因此,如果该内存位置专用于单个处理器的缓存,则 weakCompareAndSet 应该比常规的 compareAndSet 快得多.

It's apparent from the API documentation that compareAndSet acts as if it were a volatile variable. However, weakCompareAndSet is supposed to just change its specific memory location. Thus, if that memory location is exclusive to the cache of a single processor, weakCompareAndSet is supposed to be much faster than the regular compareAndSet.

我之所以这么问是因为我已经通过运行 threadnum 不同的线程,将 threadnum 从 1 变化到 8,并让 totalwork=1e9(代码是用 Scala 编写的,Scala 是一种静态编译的 JVM 语言,但在这种情况下,它的含义和字节码转换都与 Java 的同构 - 这个简短的片段应该很清楚):

I'm asking this because I've benchmarked the following methods by running threadnum different threads, varying threadnum from 1 to 8, and having totalwork=1e9 (the code is written in Scala, a statically compiled JVM language, but both its meaning and bytecode translation are isomorphic to that of Java in this case - this short snippets should be clear):

val atomic_cnt = new AtomicInteger(0)
val atomic_tlocal_cnt = new java.lang.ThreadLocal[AtomicInteger] {
  override def initialValue = new AtomicInteger(0)
}

def loop_atomic_tlocal_cas = {
  var i = 0
  val until = totalwork / threadnum
  val acnt = atomic_tlocal_cnt.get
  while (i < until) {
    i += 1
    acnt.compareAndSet(i - 1, i)
  }
  acnt.get + i
}

def loop_atomic_weakcas = {
  var i = 0
  val until = totalwork / threadnum
  val acnt = atomic_cnt
  while (i < until) {
    i += 1
    acnt.weakCompareAndSet(i - 1, i)
  }
  acnt.get + i
}

def loop_atomic_tlocal_weakcas = {
  var i = 0
  val until = totalwork / threadnum
  val acnt = atomic_tlocal_cnt.get
  while (i < until) {
    i += 1
    acnt.weakCompareAndSet(i - 1, i)
  }
  acnt.get + i
}

在具有 4 个双 2.8 GHz 内核和 2.67 GHz 4 核 i7 处理器的 AMD 上.JVM 是 Sun Server Hotspot JVM 1.6.结果显示没有性能差异.

on an AMD with 4 dual 2.8 GHz cores, and a 2.67 GHz 4-core i7 processor. The JVM is Sun Server Hotspot JVM 1.6. The results show no performance difference.

  • 线程数:1

运行时间:(显示最后 3 个)7504.562 7502.817 7504.626(平均值 = 7415.637 分钟 = 7147.628 最大值 = 7504.886)

Run times: (showing last 3) 7504.562 7502.817 7504.626 (avg = 7415.637 min = 7147.628 max = 7504.886 )

  • 线程数:2

运行时间:(显示最后 3 个)3751.553 3752.589 3751.519(平均值 = 3713.5513 最小值 = 3574.708 最大值 = 3752.949)

Run times: (showing last 3) 3751.553 3752.589 3751.519 (avg = 3713.5513 min = 3574.708 max = 3752.949 )

  • 线程数:4

运行时间:(显示最后 3 个)1890.055 1889.813 1890.047(平均值 = 2065.7207 分钟 = 1804.652 最大值 = 3755.852)

Run times: (showing last 3) 1890.055 1889.813 1890.047 (avg = 2065.7207 min = 1804.652 max = 3755.852 )

  • 线程数:8

运行时间:(显示最后 3 个)960.12 989.453 970.842(平均值 = 1058.8776 分钟 = 940.492 最大值 = 1893.127)

Run times: (showing last 3) 960.12 989.453 970.842 (avg = 1058.8776 min = 940.492 max = 1893.127 )

  • 线程数:1

运行时间:(显示最后 3 个)7325.425 7057.03 7325.407(平均值 = 7231.8682 最小值 = 7057.03 最大值 = 7325.45)

Run times: (showing last 3) 7325.425 7057.03 7325.407 (avg = 7231.8682 min = 7057.03 max = 7325.45 )

  • 线程数:2

运行时间:(显示最后 3 个)3663.21 3665.838 3533.406(平均值 = 3607.2149 最小值 = 3529.177 最大值 = 3665.838)

Run times: (showing last 3) 3663.21 3665.838 3533.406 (avg = 3607.2149 min = 3529.177 max = 3665.838 )

  • 线程数:4

运行时间:(显示最后 3 个)3664.163 1831.979 1835.07(平均值 = 2014.2086 最小值 = 1797.997 最大值 = 3664.163)

Run times: (showing last 3) 3664.163 1831.979 1835.07 (avg = 2014.2086 min = 1797.997 max = 3664.163 )

  • 线程数:8

运行时间:(显示最后 3 个)940.504 928.467 921.376(平均值 = 943.665 分钟 = 919.985 最大值 = 997.681)

Run times: (showing last 3) 940.504 928.467 921.376 (avg = 943.665 min = 919.985 max = 997.681 )

  • 线程数:1

运行时间:(显示最后 3 个)7502.876 7502.857 7502.933(平均值 = 7414.8132 最小值 = 7145.869 最大值 = 7502.933)

Run times: (showing last 3) 7502.876 7502.857 7502.933 (avg = 7414.8132 min = 7145.869 max = 7502.933 )

  • 线程数:2

运行时间:(显示最后 3 个)3752.623 3751.53 3752.434(平均值 = 3710.1782 最小值 = 3574.398 最大值 = 3752.623)

Run times: (showing last 3) 3752.623 3751.53 3752.434 (avg = 3710.1782 min = 3574.398 max = 3752.623 )

  • 线程数:4

运行时间:(显示最后 3 个)1876.723 1881.069 1876.538(平均值 = 4110.4221 最小值 = 1804.62 最大值 = 12467.351)

Run times: (showing last 3) 1876.723 1881.069 1876.538 (avg = 4110.4221 min = 1804.62 max = 12467.351 )

  • 线程数:8

运行时间:(显示最后 3 个)959.329 1010.53 969.767(平均值 = 1072.8444 最小值 = 959.329 最大值 = 1880.049)

Run times: (showing last 3) 959.329 1010.53 969.767 (avg = 1072.8444 min = 959.329 max = 1880.049 )

  • 线程数:1

运行时间:(显示最后 3 个)8138.3175 8130.0044 8130.1535(平均值 = 8119.2888 最小值 = 8049.6497 最大值 = 8150.1950)

Run times: (showing last 3) 8138.3175 8130.0044 8130.1535 (avg = 8119.2888 min = 8049.6497 max = 8150.1950 )

  • 线程数:2

运行时间:(显示最后 3 个)4067.7399 4067.5403 4068.3747(平均值 = 4059.6344 最小值 = 4026.2739 最大值 = 4068.5455)

Run times: (showing last 3) 4067.7399 4067.5403 4068.3747 (avg = 4059.6344 min = 4026.2739 max = 4068.5455 )

  • 线程数:4

运行时间:(显示最后 3 个)2033.4389 2033.2695 2033.2918(平均值 = 2030.5825 分钟 = 2017.6880 最大值 = 2035.0352)

Run times: (showing last 3) 2033.4389 2033.2695 2033.2918 (avg = 2030.5825 min = 2017.6880 max = 2035.0352 )

  • 线程数:1

运行时间:(显示最后 3 个)8130.5620 8129.9963 8132.3382(平均值 = 8114.0052 最小值 = 8042.0742 最大值 = 8132.8542)

Run times: (showing last 3) 8130.5620 8129.9963 8132.3382 (avg = 8114.0052 min = 8042.0742 max = 8132.8542 )

  • 线程数:2

运行时间:(显示最后 3 个)4066.9559 4067.0414 4067.2080(平均值 = 4086.0608 最小值 = 4023.6822 最大值 = 4335.1791)

Run times: (showing last 3) 4066.9559 4067.0414 4067.2080 (avg = 4086.0608 min = 4023.6822 max = 4335.1791 )

  • 线程数:4

运行时间:(显示最后 3 个)2034.6084 2169.8127 2034.5625(平均值 = 2047.7025 分钟 = 2032.8131 最大值 = 2169.8127)

Run times: (showing last 3) 2034.6084 2169.8127 2034.5625 (avg = 2047.7025 min = 2032.8131 max = 2169.8127 )

  • 线程数:1

运行时间:(显示最后 3 个)8132.5267 8132.0299 8132.2415(平均值 = 8114.9328 最小值 = 8043.3674 最大值 = 8134.0418)

Run times: (showing last 3) 8132.5267 8132.0299 8132.2415 (avg = 8114.9328 min = 8043.3674 max = 8134.0418 )

  • 线程数:2

运行时间:(显示最后 3 个)4066.5924 4066.5797 4066.6519(平均值 = 4059.1911 分钟 = 4025.0703 最大值 = 4066.8547)

Run times: (showing last 3) 4066.5924 4066.5797 4066.6519 (avg = 4059.1911 min = 4025.0703 max = 4066.8547 )

  • 线程数:4

运行时间:(显示最后 3 个)2033.2614 2035.5754 2036.9110(平均值 = 2033.2958 最小值 = 2023.5082 最大值 = 2038.8750)

Run times: (showing last 3) 2033.2614 2035.5754 2036.9110 (avg = 2033.2958 min = 2023.5082 max = 2038.8750 )

虽然上面示例中的线程局部变量可能会出现在相同的缓存行中,但在我看来,常规 CAS 与其弱版本之间没有明显的性能差异.

While it's possible that thread locals in the example above end up in the same cache lines, it seems to me that there is no observable performance difference between regular CAS and its weak version.

这可能意味着,事实上,弱比较和交换充当完全成熟的内存栅栏,即就像一个易失性变量一样.

This could mean that, in fact, a weak compare and swap acts as fully fledged memory fence, i.e. acts as if it were a volatile variable.

问题:这个观察是否正确?另外,是否有已知的架构或 Java 发行版,弱比较和设置实际上更快?如果不是,首先使用弱 CAS 有什么好处?

Question: Is this observation correct? Also, is there a known architecture or Java distribution for which a weak compare and set is actually faster? If not, what is the advantage of using a weak CAS in the first place?

推荐答案

弱比较和交换可以充当完全可变的变量,这当然取决于 JVM 的实现.事实上,如果在某些架构上不可能以比普通 CAS 性能更高的方式实现弱 CAS,我不会感到惊讶.在这些架构上,很可能弱 CAS 的实现与完整 CAS 完全相同.或者可能只是您的 JVM 没有进行太多优化来使弱 CAS 变得特别快,所以 当前 实现只是调用一个完整的 CAS,因为它实现起来很快,未来的版本将对此进行改进.

A weak compare and swap could act as a full volatile variable, depending on the implementation of the JVM, sure. In fact, I wouldn't be surprised if on certain architectures it is not possible to implement a weak CAS in a notably more performant way than the normal CAS. On these architectures, it may well be the case that weak CASes are implemented exactly the same as a full CAS. Or it might simply be that your JVM has not had much optimisation put into making weak CASes particularly fast, so the current implementation just invokes a full CAS because it's quick to implement, and a future version will refine this.

JLS 只是说弱 CAS 没有建立happens-before关系,所以只是没有保证它引起的修改在其他线程.在这种情况下,您只能保证比较和设置操作是原子的,但不能保证(潜在的)新值的可见性.这与保证它不会被看到不同,因此您的测试与此一致.

The JLS simply says that a weak CAS does not establish a happens-before relationship, so it's simply that there is no guarantee that the modification it causes is visible in other threads. All you get in this case is the guarantee that the compare-and-set operation is atomic, but with no guarantees about the visibility of the (potentially) new value. That's not the same as guaranteeing that it won't be seen, so your tests are consistent with this.

一般来说,尽量避免通过实验对与并发相关的行为做出任何结论.有很多变量需要考虑,如果您不遵循 JLS 保证正确的内容,那么您的程序可能随时中断(也许在不同的架构上,也许在更多积极的优化是由代码布局的轻微变化引起的,也许在不存在的 JVM 的未来构建中,等等).永远没有理由假设您可以逃脱某些声明无法保证的事情,因为实验表明它有效".

In general, try to avoid making any conclusions about concurrency-related behaviour through experimentation. There are so many variables to take into account, that if you don't follow what the JLS guarantees to be correct, then your program could break at any time (perhaps on a different architecture, perhaps under more aggressive optimisation that's prompted by a slight change in the layout of your code, perhaps under future builds of the JVM that don't exist yet, etc.). There's never a reason to assume you can get away with something that's stated not to be guaranteed, because experiments show that "it works".

这篇关于Java 比较和交换语义和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆