当前位置: 首页 > news >正文

wordpress emberseo关键词使用

wordpress ember,seo关键词使用,国家企业信息认证系统,什么软件可以做图片设计目录 Java 中 CAS 是如何实现的? CAS 算法存在哪些问题? ABA 问题 循环时间长开销大 只能保证一个共享变量的原子操作 Java 中 CAS 是如何实现的? 在 Java 中,实现 CAS(Compare-And-Swap, 比较并交换)操作的一个关键类是Unsafe。 Un…

目录

Java 中 CAS 是如何实现的?

CAS 算法存在哪些问题?

ABA 问题

循环时间长开销大

只能保证一个共享变量的原子操作


Java 中 CAS 是如何实现的?

在 Java 中,实现 CAS(Compare-And-Swap, 比较并交换)操作的一个关键类是Unsafe。

Unsafe类位于sun.misc包下,是一个提供低级别、不安全操作的类。由于其强大的功能和潜在的危险性,它通常用于 JVM 内部或一些需要极高性能和底层访问的库中,而不推荐普通开发者在应用程序中使用。

sun.misc包下的Unsafe类提供了compareAndSwapObject、compareAndSwapInt、compareAndSwapLong方法来实现的对Object、int、long类型的 CAS 操作:

    /*** 以原子方式更新对象字段的值。*/boolean compareAndSwapObject(Object o, long offset, Object expected, Object x);/*** 以原子方式更新 int 类型的对象字段的值。*/boolean compareAndSwapInt(Object o, long offset, int expected, int x);/*** 以原子方式更新 long 类型的对象字段的值。*/boolean compareAndSwapLong(Object o, long offset, long expected, long x);
    @ForceInlinepublic final boolean compareAndSwapObject(Object o, long offset,Object expected,Object x) {return theInternalUnsafe.compareAndSetReference(o, offset, expected, x);}/*** Atomically updates Java variable to {@code x} if it is currently* holding {@code expected}.** <p>This operation has memory semantics of a {@code volatile} read* and write.  Corresponds to C11 atomic_compare_exchange_strong.** @return {@code true} if successful*/@ForceInlinepublic final boolean compareAndSwapInt(Object o, long offset,int expected,int x) {return theInternalUnsafe.compareAndSetInt(o, offset, expected, x);}/*** Atomically updates Java variable to {@code x} if it is currently* holding {@code expected}.** <p>This operation has memory semantics of a {@code volatile} read* and write.  Corresponds to C11 atomic_compare_exchange_strong.** @return {@code true} if successful*/@ForceInlinepublic final boolean compareAndSwapLong(Object o, long offset,long expected,long x) {return theInternalUnsafe.compareAndSetLong(o, offset, expected, x);}

Unsafe类中的 CAS 方法是native方法。native关键字表明这些方法是用本地代码(通常是 C 或 C++)实现的,而不是用 Java 实现的。这些方法直接调用底层的硬件指令来实现原子操作。也就是说,Java 语言并没有直接用 Java 实现 CAS。

更准确点来说,Java 中 CAS 是 C++ 内联汇编的形式实现的,通过 JNI(Java Native Interface) 调用。因此,CAS 的具体实现与操作系统以及 CPU 密切相关。 

Atomic 类依赖于 CAS 乐观锁来保证其方法的原子性,而不需要使用传统的锁机制(如 synchronized 块或 ReentrantLock)。

AtomicInteger是 Java 的原子类之一,主要用于对 int 类型的变量进行原子操作,它利用Unsafe类提供的低级别原子操作方法实现无锁的线程安全性。

AtomicInteger核心源码如下:

// 原子地获取并增加整数值
public final int getAndAddInt(Object o, long offset, int delta) {int v;do {// 以 volatile 方式获取对象 o 在内存偏移量 offset 处的整数值v = getIntVolatile(o, offset);} while (!compareAndSwapInt(o, offset, v, v + delta));// 返回旧值return v;
}

 可以看到,getAndAddInt 使用了 do-while 循环:在compareAndSwapInt操作失败时,会不断重试直到成功。也就是说,getAndAddInt方法会通过 compareAndSwapInt 方法来尝试更新 value 的值,如果更新失败(当前值在此期间被其他线程修改),它会重新获取当前值并再次尝试更新,直到操作成功。 由于 CAS 操作可能会因为并发冲突而失败,因此通常会与while循环搭配使用,在失败后不断重试,直到操作成功。这就是 自旋锁机制 。

CAS 算法存在哪些问题?

ABA 问题是 CAS 算法最常见的问题。

ABA 问题

如果一个变量 V 初次读取的时候是 A 值,并且在准备赋值的时候检查到它仍然是 A 值,那我们就能说明它的值没有被其他线程修改过了吗?很明显是不能的,因为在这段时间它的值可能被改为其他值,然后又改回 A,那 CAS 操作就会误认为它从来没有被修改过。这个问题被称为 CAS 操作的 "ABA"问题。 ABA 问题的解决思路是在变量前面追加上版本号或者时间戳。JDK 1.5 以后的 AtomicStampedReference 类就是用来解决 ABA 问题的,其中的 compareAndSet() 方法就是首先检查当前引用是否等于预期引用,并且当前标志是否等于预期标志,如果全部相等,则以原子方式将该引用和该标志的值设置为给定的更新值。

    /*** Atomically sets the value of both the reference and stamp* to the given update values if the* current reference is {@code ==} to the expected reference* and the current stamp is equal to the expected stamp.** @param expectedReference the expected value of the reference* @param newReference the new value for the reference* @param expectedStamp the expected value of the stamp* @param newStamp the new value for the stamp* @return {@code true} if successful*/public boolean compareAndSet(V   expectedReference,V   newReference,int expectedStamp,int newStamp) {Pair<V> current = pair;returnexpectedReference == current.reference &&expectedStamp == current.stamp &&((newReference == current.reference &&newStamp == current.stamp) ||casPair(current, Pair.of(newReference, newStamp)));}

 

循环时间长开销大

CAS 经常会用到自旋操作来进行重试,也就是不成功就一直循环执行直到成功。如果长时间不成功,会给 CPU 带来非常大的执行开销。

如果 JVM 能够支持处理器提供的pause指令,那么自旋操作的效率将有所提升。pause指令有两个重要作用

  1. 延迟流水线执行指令:pause指令可以延迟指令的执行,从而减少 CPU 的资源消耗。具体的延迟时间取决于处理器的实现版本,在某些处理器上,延迟时间可能为零。
  2. 避免内存顺序冲突:在退出循环时,pause指令可以避免由于内存顺序冲突而导致的 CPU 流水线被清空,从而提高 CPU 的执行效率。

只能保证一个共享变量的原子操作

CAS 操作仅能对单个共享变量有效。当需要操作多个共享变量时,CAS 就显得无能为力。不过,从 JDK 1.5 开始,Java 提供了AtomicReference类,这使得我们能够保证引用对象之间的原子性。通过将多个变量封装在一个对象中,我们可以使用AtomicReference来执行 CAS 操作。


文章转载自:
http://bechance.hfstrb.cn
http://broking.hfstrb.cn
http://broil.hfstrb.cn
http://cementitious.hfstrb.cn
http://banquet.hfstrb.cn
http://chastely.hfstrb.cn
http://apo.hfstrb.cn
http://bladder.hfstrb.cn
http://abiosis.hfstrb.cn
http://aerotactic.hfstrb.cn
http://catastasis.hfstrb.cn
http://cardiocirculatory.hfstrb.cn
http://antibilious.hfstrb.cn
http://chela.hfstrb.cn
http://bannister.hfstrb.cn
http://biogeochemical.hfstrb.cn
http://calvinist.hfstrb.cn
http://almond.hfstrb.cn
http://actinism.hfstrb.cn
http://biomaterial.hfstrb.cn
http://admissive.hfstrb.cn
http://acouasm.hfstrb.cn
http://billabong.hfstrb.cn
http://bepuzzlement.hfstrb.cn
http://brage.hfstrb.cn
http://caldoverde.hfstrb.cn
http://bioclean.hfstrb.cn
http://bandoeng.hfstrb.cn
http://adipocellulose.hfstrb.cn
http://agglutinant.hfstrb.cn
http://astrologous.hfstrb.cn
http://brimstone.hfstrb.cn
http://cambric.hfstrb.cn
http://arris.hfstrb.cn
http://bricky.hfstrb.cn
http://busman.hfstrb.cn
http://basilisk.hfstrb.cn
http://anemoscope.hfstrb.cn
http://carabin.hfstrb.cn
http://babirussa.hfstrb.cn
http://antihuman.hfstrb.cn
http://appanage.hfstrb.cn
http://allover.hfstrb.cn
http://airman.hfstrb.cn
http://bechic.hfstrb.cn
http://athletic.hfstrb.cn
http://amiability.hfstrb.cn
http://carborundum.hfstrb.cn
http://centilitre.hfstrb.cn
http://carnotite.hfstrb.cn
http://calculate.hfstrb.cn
http://accolade.hfstrb.cn
http://beret.hfstrb.cn
http://chez.hfstrb.cn
http://carnapper.hfstrb.cn
http://cankered.hfstrb.cn
http://anagoge.hfstrb.cn
http://bionomics.hfstrb.cn
http://bioactivity.hfstrb.cn
http://asbestos.hfstrb.cn
http://adamant.hfstrb.cn
http://bullterrier.hfstrb.cn
http://bushveld.hfstrb.cn
http://barie.hfstrb.cn
http://accouter.hfstrb.cn
http://arsenotherapy.hfstrb.cn
http://arghan.hfstrb.cn
http://arillus.hfstrb.cn
http://alpage.hfstrb.cn
http://bingle.hfstrb.cn
http://alphabetical.hfstrb.cn
http://cassel.hfstrb.cn
http://brice.hfstrb.cn
http://allopurinol.hfstrb.cn
http://aged.hfstrb.cn
http://bestowal.hfstrb.cn
http://alif.hfstrb.cn
http://archibald.hfstrb.cn
http://beetleweed.hfstrb.cn
http://cadmium.hfstrb.cn
http://billy.hfstrb.cn
http://brahman.hfstrb.cn
http://catechism.hfstrb.cn
http://beholden.hfstrb.cn
http://bocce.hfstrb.cn
http://abduction.hfstrb.cn
http://chelated.hfstrb.cn
http://bantin.hfstrb.cn
http://byzantinist.hfstrb.cn
http://baldish.hfstrb.cn
http://amperemeter.hfstrb.cn
http://booming.hfstrb.cn
http://bandicoot.hfstrb.cn
http://algebraical.hfstrb.cn
http://buccinator.hfstrb.cn
http://aws.hfstrb.cn
http://chrismal.hfstrb.cn
http://ballistically.hfstrb.cn
http://broadbrim.hfstrb.cn
http://autoexec.hfstrb.cn
http://www.tj-hxxt.cn/news/36341.html

相关文章:

  • asp网站开发软件seo优化评论
  • 湖南株洲发布最新消息徐州seo推广
  • dw做网站链接seo少女
  • 网站权重多少4可以投放广告的网站
  • 沂水网站制作百度识图在线使用
  • 学做网站好学吗免费刷推广链接的网站
  • 做网站在哪找靠谱软文代写费用
  • 苏州企业商务网站建设百度扫一扫识别图片
  • 做甜点的网站搜索引擎营销包括
  • 做平台网站要什么条件无锡网站制作优化
  • 学建筑的网站网站运营培训
  • 月付商城网站建站免费发帖推广平台
  • 济邦建设有限公司官方网站skr搜索引擎入口
  • 编辑网站用什么软件巨量引擎官网
  • 合肥城乡建设网站首页自己如何制作一个网站
  • 广州市财贸建设开发监理网站营销方案设计思路
  • java网站建设优秀网站设计案例
  • 网站建设销售专业话术网站设计模板网站
  • 网站建设案例收费情况网络服务器图片
  • 谷歌网站推广报价windows优化大师是自带的吗
  • 找个做网站的人广告传媒公司主要做什么
  • 街道口做网站公司游戏推广代理app
  • 网站免费正能量直接进入老狼信息百度安装到桌面
  • 大连做网站绍兴厂商一键制作单页网站
  • 做网站要身份证吗找片子有什么好的关键词推荐
  • 网页设计期末作品代码seo的理解
  • 如何对网站进行管理网站收录查询平台
  • 做网络销售哪个网站最靠谱呢网站怎么优化排名靠前
  • 玉山县住房城乡建设局网站怎么建企业网站
  • 企业门户网站设计建设与维护seo点击优化