//java.util.concurrent.atomic.AtomicInteger.java public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); }
//sun.misc.Unsafe.java public final native boolean compareAndSwapInt(Object o, long offset, int expected, int x);
// http://hg.openjdk.java.net/jdk8u/jdk8u/ hotspotsrcsharevmprimsunsafe.cpp // These are the methods prior to the JSR 166 changes in 1.6.0 static JNINativeMethod methods_15[] = { //... {CC "compareAndSwapInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)}, //... }; UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) UnsafeWrapper("Unsafe_CompareAndSwapInt"); oop p = JNIHandles::resolve(obj); jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); return (jint)(Atomic::cmpxchg(x, addr, e)) == e; UNSAFE_END
// http://hg.openjdk.java.net/jdk8u/jdk8u/ hotspotsrcsharevm untimeatomic.cpp unsigned Atomic::cmpxchg(unsigned int exchange_value, volatile unsigned int* dest, unsigned int compare_value) { assert(sizeof(unsigned int) == sizeof(jint), "more work to do"); return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value); }
// http://hg.openjdk.java.net/jdk8u/jdk8u/ hotspotsrcos_cpuwindows_x86vmatomic_windows_x86.inline.hpp inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { // alternative for InterlockedCompareExchange int mp = os::is_MP(); __asm { mov edx, dest mov ecx, exchange_value mov eax, compare_value LOCK_IF_MP(mp) cmpxchg dword ptr [edx], ecx } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/72918.html
摘要:本篇我们将以的公平锁为例来详细看看使用获取独占锁的流程。本文中的源码基于。由于本篇我们分析的是独占锁,同一时刻,锁只能被一个线程所持有。由于在整个抢锁过程中,我们都是不响应中断的。 前言 AQS(AbstractQueuedSynchronizer)是JAVA中众多锁以及并发工具的基础,其底层采用乐观锁,大量使用了CAS操作, 并且在冲突时,采用自旋方式重试,以实现轻量级和高效地获取锁...
ConcurrentHashMap源码分析_JDK1.8版本 声明 文章均为本人技术笔记,转载请注明出处[1] https://segmentfault.com/u/yzwall[2] blog.csdn.net/j_dark/ JDK1.6版本 ConcurrentHashMap结构 showImg(https://segmentfault.com/img/remote/146000000900...
摘要:等到所有子线程都执行完后即,会主调用线程,然后主调用线程就会从函数返回,继续后余动作。 原理剖析(第 005 篇)AQS工作原理分析 - 一、大致介绍 1、前面章节讲解了一下CAS,简单讲就是cmpxchg+lock的原子操作; 2、而在谈到并发操作里面,我们不得不谈到AQS,JDK的源码里面好多并发的类都是通过Sync的内部类继承AQS而实现出五花八门的功能; 3、本章节就和大家分享...
阅读 2136·2021-11-22 15:22
阅读 1285·2021-11-11 16:54
阅读 1807·2021-09-23 11:32
阅读 3007·2021-09-22 10:02
阅读 1770·2019-08-30 12:59
阅读 1085·2019-08-29 16:27
阅读 621·2019-08-29 13:21
阅读 2463·2019-08-28 17:57