什么是自适应旋转 w.r.t 锁获取? [英] What is adaptive spinning w.r.t lock acquisition?

查看:27
本文介绍了什么是自适应旋转 w.r.t 锁获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 6 在提高内在锁性能方面的四个主要变化之一是Adapative Spinning 技术.究竟什么是自适应旋转?它是 spinlocks 和互斥锁的组合吗?有人能用简单易懂的方式解释这种策略的用处,JDK6 以后默认提供这种策略.

One of the four major changes brought to java 6 with regards to improving the performance of intrinsic locks is Adapative Spinning technique. What exactly is adaptive spinning? Is it a combination of spinlocks and mutex lock? Can someone explain in an easy to understand way the usefulness of this strategy which by default is available from JDK6 onwards.

推荐答案

究竟什么是自适应旋转?

What exactly is adaptive spinning?

引用此Java 6 性能页面:

自适应自旋是一种优化技术,其中尝试竞争同步进入操作的线程使用两阶段自旋然后阻塞策略.这种技术使线程能够避免影响性能的不良影响,例如上下文切换和重新填充 翻译后备缓冲区 (TLB).它是自适应的",因为自旋的持续时间由基于诸如同一监视器上最近自旋尝试的成功率和/或失败率以及当前锁所有者的状态等因素的策略决策确定.

Adaptive spinning is an optimization technique where a two-phase spin-then-block strategy is used by threads attempting a contended synchronized enter operation. This technique enables threads to avoid undesirable effects that impact performance such as context switching and repopulation of Translation Lookaside Buffers (TLBs). It is "adaptive" because the duration of the spin is determined by policy decisions based on factors such as the rate of success and/or failure of recent spin attempts on the same monitor and the state of the current lock owner.

所以线程最初尝试旋转几次,试图在实际阻塞之前获取锁.然后在将来它使用以前的成功/失败指标来自适应地确定它是否应该尝试旋转或阻塞.旋转会浪费 CPU 时间,而阻塞可能会导致上下文切换,并且线程可能会等待比必要时间更长的时间.目标是尝试根据过去的行为优化这两个问题.

So threads initially attempt to spin a couple of times trying to acquire a lock before actually blocking. Then in the future it uses previous success/fail metrics to adaptively determine whether it should try spinning or blocking. Spinning wastes CPU time while blocking may cause a context switch and the thread may wait for a longer amount of time than necessary. The goal is trying to optimize both of these issues based on past behavior.

有关更多详细信息,性能文档参考了标题为 Java SE 6 (HotSpot) 中的同步的演示文稿通过戴夫骰子.第一张幻灯片的标题是竞争成本(可扩展性 + 延迟)":

For more details, the performance doc references this presentation entitled Synchronization in Java SE 6 (HotSpot) by Dave Dice. The first slide is titled "Contended costs (scalability + latency)":

  • 上下文切换非常昂贵
  • 无界旋转是不可接受的
  • 通过自适应旋转寻址

在演示文稿的后面有一张题为自适应旋转"的幻灯片:

Later in the presentation there is the slide entitled "Adaptive Spinning":

  • 自旋然后阻止策略
    • 尝试通过在 [多处理器] 系统上旋转来避免上下文切换
    • 维护每个显示器
    • 根据旋转成功/失败比率的近期历史变化

    有趣的东西.

    这篇关于什么是自适应旋转 w.r.t 锁获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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