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

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

问题描述

适应性旋转技术是Java 6在改善固有锁性能方面的四个主要变化之一.自适应旋转到底是什么?它是自旋锁和互斥锁的组合吗?有人能以一种易于理解的方式解释该策略的有效性吗?默认情况下,此策略可从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中的同步(热点)戴夫·迪斯(Dave Dice).第一张幻灯片的标题为竞争成本(可扩展性+延迟)":

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":

  • 先动后阻止策略
    • 尝试通过在[多处理器]系统上旋转来避免上下文切换
    • Spin-then-block strategy
      • Try to avoid context switch by spinning on [multi-processor] systems
      • 由每个监视器维护
      • 基于最近的旋转成功/失败率历史记录

      有趣的东西.

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

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