1. CAS简介

  在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令。它将内存位置的内容与给定值进行比较,只有在相同的情况下,将该内存位置的内容修改为新的给定值。这是作为单个原子操作完成的。

  原子性保证新值基于最新信息计算;如果该值在同一时间被另一个线程更新,则写入将失败。操作结果必须说明是否进行替换;这可以通过一个简单的布尔响应(这个变体通常称为比较和设置),或通过返回从内存位置读取的值来完成。

  查看JUC(java.util.concurrent)下的atomic包:

2. CAS在Java中的应用

  以AtomicInteger为例:

  1. package java.util.concurrent.atomic;
  2. import java.util.function.IntUnaryOperator;
  3. import java.util.function.IntBinaryOperator;
  4. import sun.misc.Unsafe;
  5. /**
  6. * An {@code int} value that may be updated atomically. See the
  7. * {@link java.util.concurrent.atomic} package specification for
  8. * description of the properties of atomic variables. An
  9. * {@code AtomicInteger} is used in applications such as atomically
  10. * incremented counters, and cannot be used as a replacement for an
  11. * {@link java.lang.Integer}. However, this class does extend
  12. * {@code Number} to allow uniform access by tools and utilities that
  13. * deal with numerically-based classes.
  14. *
  15. * @since 1.5
  16. * @author Doug Lea
  17. */
  18. public class AtomicInteger extends Number implements java.io.Serializable {
  19. private static final long serialVersionUID = 6214790243416807050L;
  20. // setup to use Unsafe.compareAndSwapInt for updates
  21. private static final Unsafe unsafe = Unsafe.getUnsafe();
  22. private static final long valueOffset;
  23. static {
  24. try {
  25. valueOffset = unsafe.objectFieldOffset
  26. (AtomicInteger.class.getDeclaredField("value"));
  27. } catch (Exception ex) { throw new Error(ex); }
  28. }
  29. private volatile int value;
  30. /**
  31. * Creates a new AtomicInteger with the given initial value.
  32. *
  33. * @param initialValue the initial value
  34. */
  35. public AtomicInteger(int initialValue) {
  36. value = initialValue;
  37. }
  38. /**
  39. * Creates a new AtomicInteger with initial value {@code 0}.
  40. */
  41. public AtomicInteger() {
  42. }
  43. /**
  44. * Gets the current value.
  45. *
  46. * @return the current value
  47. */
  48. public final int get() {
  49. return value;
  50. }
  51. /**
  52. * Sets to the given value.
  53. *
  54. * @param newValue the new value
  55. */
  56. public final void set(int newValue) {
  57. value = newValue;
  58. }
  59. /**
  60. * Eventually sets to the given value.
  61. *
  62. * @param newValue the new value
  63. * @since 1.6
  64. */
  65. public final void lazySet(int newValue) {
  66. unsafe.putOrderedInt(this, valueOffset, newValue);
  67. }
  68. /**
  69. * Atomically sets to the given value and returns the old value.
  70. *
  71. * @param newValue the new value
  72. * @return the previous value
  73. */
  74. public final int getAndSet(int newValue) {
  75. return unsafe.getAndSetInt(this, valueOffset, newValue);
  76. }
  77. /**
  78. * Atomically sets the value to the given updated value
  79. * if the current value {@code ==} the expected value.
  80. *
  81. * @param expect the expected value
  82. * @param update the new value
  83. * @return {@code true} if successful. False return indicates that
  84. * the actual value was not equal to the expected value.
  85. */
  86. public final boolean compareAndSet(int expect, int update) {
  87. return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
  88. }
  89. /**
  90. * Atomically sets the value to the given updated value
  91. * if the current value {@code ==} the expected value.
  92. *
  93. * <p><a href="package-summary.html#weakCompareAndSet">May fail
  94. * spuriously and does not provide ordering guarantees</a>, so is
  95. * only rarely an appropriate alternative to {@code compareAndSet}.
  96. *
  97. * @param expect the expected value
  98. * @param update the new value
  99. * @return {@code true} if successful
  100. */
  101. public final boolean weakCompareAndSet(int expect, int update) {
  102. return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
  103. }
  104. /**
  105. * Atomically increments by one the current value.
  106. *
  107. * @return the previous value
  108. */
  109. public final int getAndIncrement() {
  110. return unsafe.getAndAddInt(this, valueOffset, 1);
  111. }
  112. /**
  113. * Atomically decrements by one the current value.
  114. *
  115. * @return the previous value
  116. */
  117. public final int getAndDecrement() {
  118. return unsafe.getAndAddInt(this, valueOffset, -1);
  119. }
  120. /**
  121. * Atomically adds the given value to the current value.
  122. *
  123. * @param delta the value to add
  124. * @return the previous value
  125. */
  126. public final int getAndAdd(int delta) {
  127. return unsafe.getAndAddInt(this, valueOffset, delta);
  128. }
  129. /**
  130. * Atomically increments by one the current value.
  131. *
  132. * @return the updated value
  133. */
  134. public final int incrementAndGet() {
  135. return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
  136. }
  137. /**
  138. * Atomically decrements by one the current value.
  139. *
  140. * @return the updated value
  141. */
  142. public final int decrementAndGet() {
  143. return unsafe.getAndAddInt(this, valueOffset, -1) - 1;
  144. }
  145. /**
  146. * Atomically adds the given value to the current value.
  147. *
  148. * @param delta the value to add
  149. * @return the updated value
  150. */
  151. public final int addAndGet(int delta) {
  152. return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
  153. }
  154. /**
  155. * Atomically updates the current value with the results of
  156. * applying the given function, returning the previous value. The
  157. * function should be side-effect-free, since it may be re-applied
  158. * when attempted updates fail due to contention among threads.
  159. *
  160. * @param updateFunction a side-effect-free function
  161. * @return the previous value
  162. * @since 1.8
  163. */
  164. public final int getAndUpdate(IntUnaryOperator updateFunction) {
  165. int prev, next;
  166. do {
  167. prev = get();
  168. next = updateFunction.applyAsInt(prev);
  169. } while (!compareAndSet(prev, next));
  170. return prev;
  171. }
  172. /**
  173. * Atomically updates the current value with the results of
  174. * applying the given function, returning the updated value. The
  175. * function should be side-effect-free, since it may be re-applied
  176. * when attempted updates fail due to contention among threads.
  177. *
  178. * @param updateFunction a side-effect-free function
  179. * @return the updated value
  180. * @since 1.8
  181. */
  182. public final int updateAndGet(IntUnaryOperator updateFunction) {
  183. int prev, next;
  184. do {
  185. prev = get();
  186. next = updateFunction.applyAsInt(prev);
  187. } while (!compareAndSet(prev, next));
  188. return next;
  189. }
  190. /**
  191. * Atomically updates the current value with the results of
  192. * applying the given function to the current and given values,
  193. * returning the previous value. The function should be
  194. * side-effect-free, since it may be re-applied when attempted
  195. * updates fail due to contention among threads. The function
  196. * is applied with the current value as its first argument,
  197. * and the given update as the second argument.
  198. *
  199. * @param x the update value
  200. * @param accumulatorFunction a side-effect-free function of two arguments
  201. * @return the previous value
  202. * @since 1.8
  203. */
  204. public final int getAndAccumulate(int x,
  205. IntBinaryOperator accumulatorFunction) {
  206. int prev, next;
  207. do {
  208. prev = get();
  209. next = accumulatorFunction.applyAsInt(prev, x);
  210. } while (!compareAndSet(prev, next));
  211. return prev;
  212. }
  213. /**
  214. * Atomically updates the current value with the results of
  215. * applying the given function to the current and given values,
  216. * returning the updated value. The function should be
  217. * side-effect-free, since it may be re-applied when attempted
  218. * updates fail due to contention among threads. The function
  219. * is applied with the current value as its first argument,
  220. * and the given update as the second argument.
  221. *
  222. * @param x the update value
  223. * @param accumulatorFunction a side-effect-free function of two arguments
  224. * @return the updated value
  225. * @since 1.8
  226. */
  227. public final int accumulateAndGet(int x,
  228. IntBinaryOperator accumulatorFunction) {
  229. int prev, next;
  230. do {
  231. prev = get();
  232. next = accumulatorFunction.applyAsInt(prev, x);
  233. } while (!compareAndSet(prev, next));
  234. return next;
  235. }
  236. //......
  237. }

  可以看出自JDK1.5就开始引入CAS来解决多线程中的并发问题。

  查看方法源码,可以看出所有的CAS操作都是通过sun.misc包下Unsafe类实现的。而sun.misc包存在于JDK的rt.jar包,是由JVM本地实现。

  Unsafe是CAS的核心类。由于Java无法直接访问底层系统,则需要通过本地(native)来访问。Unsafe可以直接操作特定内存的数,其内部方法可以像C语言的指针一样直接操作内存。

  注意:Unsafe类的所有方法都是native修饰的,即Unsafe类的所有方法都可以直接调用底层操作系统资源。

3. CAS在JUC中的应用

  以重入锁ReentrantLock为例。通过查看部分源码:

  1. public class ReentrantLock implements Lock, java.io.Serializable {
  2. private static final long serialVersionUID = 7373984872572414699L;
  3. /** Synchronizer providing all implementation mechanics */
  4. private final Sync sync;
  5. /**
  6. * Base of synchronization control for this lock. Subclassed
  7. * into fair and nonfair versions below. Uses AQS state to
  8. * represent the number of holds on the lock.
  9. */
  10. abstract static class Sync extends AbstractQueuedSynchronizer {
  11. private static final long serialVersionUID = -5179523762034025860L;
  12. /**
  13. * Performs {@link Lock#lock}. The main reason for subclassing
  14. * is to allow fast path for nonfair version.
  15. */
  16. abstract void lock();
  17. /**
  18. * Performs non-fair tryLock. tryAcquire is implemented in
  19. * subclasses, but both need nonfair try for trylock method.
  20. */
  21. final boolean nonfairTryAcquire(int acquires) {
  22. final Thread current = Thread.currentThread();
  23. int c = getState();
  24. if (c == 0) {
  25. if (compareAndSetState(0, acquires)) {
  26. setExclusiveOwnerThread(current);
  27. return true;
  28. }
  29. }
  30. else if (current == getExclusiveOwnerThread()) {
  31. int nextc = c + acquires;
  32. if (nextc < 0) // overflow
  33. throw new Error("Maximum lock count exceeded");
  34. setState(nextc);
  35. return true;
  36. }
  37. return false;
  38. }
  39. //......
  40. }
  41. //......
  42. }

  可以看出,内部抽象类Sync继承自AbstractQueuedSynchronizer类。AbstractQueuedSynchronizer作为Java多种锁的父类,有很多地方通过CAS操作来提高并发效率。查看AbstractQueuedSynchronizer部分源码:

  1. /**
  2. * Inserts node into queue, initializing if necessary. See picture above.
  3. * @param node the node to insert
  4. * @return node's predecessor
  5. */
  6. private Node enq(final Node node) {
  7. for (;;) {
  8. Node t = tail;
  9. if (t == null) { // Must initialize
  10. if (compareAndSetHead(new Node()))
  11. tail = head;
  12. } else {
  13. node.prev = t;
  14. if (compareAndSetTail(t, node)) {
  15. t.next = node;
  16. return t;
  17. }
  18. }
  19. }
  20. }

  可以看出在上述的同步队列的入队操作时,在多线程环境下,对其头尾节点的操作都有可能失败,失败后通过自旋操作再次尝试,直到成功,这也是一种乐观锁的实现。

4. CAS缺点

  • 循环时间长,CPU开销大
  • 只能保证一个共享变量的原子操作
  • 引出ABA问题

5. ABA问题

  比如说一个线程1从内存位置V中取出A,另一个线程2也从内存中取出A,线程2将A变成了B,然后将V位置的数据变成A,这时候线程1进行CAS操作发现内存中仍然是A,那么线程1操作成功。尽管线程1的CAS操作成功,但是不代表这个过程就是没有问题的。

  如果链表的头在变化了两次后恢复了原值,但是不代表链表就没有变化。

  所以JAVA中提供了AtomicStampedReferenceAtomicMarkableReference来处理ABA问题,主要是在对象中额外再增加一个标记来标识对象是否有过变更。

【面试专栏】JAVA CAS(Conmpare And Swap)原理的更多相关文章

  1. 浅谈CAS(Compare and Swap) 原理

    浅谈CAS原理java并发编程也研究了一段时间了,对CAS的原理总是不太理解,今天再研究了一下,记录一些自己的理解.    说到CAS,再java中的某些情况下,甚至jdk1.5以后的大多数情况,并发 ...

  2. 【面试专栏】JAVA锁机制

    1. 悲观锁 / 乐观锁   在Java和数据库中都存在悲观锁和乐观锁的应用.Mysql锁机制中的悲观锁和乐观锁请查看:   Mysql锁机制--悲观锁和乐观锁   悲观锁:在获得数据时先加锁,只到数 ...

  3. JAVA CAS原理深度分析-转载

    参考文档: http://www.blogjava.net/xylz/archive/2010/07/04/325206.html http://blog.hesey.net/2011/09/reso ...

  4. JAVA CAS原理

    转自: http://blog.csdn.net/hsuxu/article/details/9467651 CAS CAS: Compare and Swap java.util.concurren ...

  5. 【转】JAVA CAS原理深度分析

    java.util.concurrent包完全建立在CAS之上的,没有CAS就不会有此包.可见CAS的重要性. CAS CAS:Compare and Swap, 翻译成比较并交换. java.uti ...

  6. JAVA CAS原理深度分析

    参考文档: http://www.blogjava.net/xylz/archive/2010/07/04/325206.html http://blog.hesey.net/2011/09/reso ...

  7. JAVA CAS原理深度分析(转)

    看了一堆文章,终于把JAVA CAS的原理深入分析清楚了. 感谢GOOGLE强大的搜索,借此挖苦下百度,依靠百度什么都学习不到! 参考文档: http://www.blogjava.net/xylz/ ...

  8. 【Java并发编程】9、非阻塞同步算法与CAS(Compare and Swap)无锁算法

    转自:http://www.cnblogs.com/Mainz/p/3546347.html?utm_source=tuicool&utm_medium=referral 锁(lock)的代价 ...

  9. JAVA CAS原理浅谈

    java.util.concurrent包完全建立在CAS之上的,没有CAS就不会有此包.可见CAS的重要性. CAS CAS:Compare and Swap, 翻译成比较并交换. java.uti ...

随机推荐

  1. ubuntu 18.04安装RTL8821CE无线网卡驱动

    疫情期间闲下来无聊,把办公室的旧机器装了ubuntu,但是无法连接无线网. 打开终端 #查看无线网卡信息. -i 是不区分大小写 tjj@ubuntu:~/Documents$ lspci | gre ...

  2. Nmap详解

    扫描方式 -Pn/-P0:扫描前不用ping测试目标是否可达,默认所有目标端口都可达 -sT:TCP Connect扫描,进行完整的TCP三次握手,该类型扫描已被检测,且会在目标日志中记录大量连接请求 ...

  3. [原题复现]2019强网杯WEB-随便注(多种方法)

    简介 原题复现:https://gitee.com/xiaohua1998/qwb_2019_supersqli  考察知识点:SQL注入漏洞-堆叠注入  线上平台:https://buuoj.cn( ...

  4. Mac用户好帮手CrossOver:耗时少,效率高

    Mac系统仅适配自己的硬件,它的软件需要通过app store购买,所以很多Mac用户也为之烦恼.这种模式优点是稳定性与性能超强发挥,缺点也显而易见. 那该如何解决这一困扰呢?一般,我们会选择安装虚拟 ...

  5. Mac读写ntfs软件究竟哪一款适合我们?

    生活中我们免不了会使用一些硬盘设备来存储文件或者是数据,然而绝大多数的移动硬盘都是ntfs格式.Mac读写ntfs软件有很多,究竟哪一款适合我们? 首先,我们一起了解一下什么是ntfs格式.ntfs, ...

  6. 【爬虫】基于PUPPETEER页面爬虫

    一.简介 本文简单介绍一下如何用puppeteer抓取页面数据. 二.下载 npm install puppeteer --save-dev npm install typescrip --save- ...

  7. Idea中如何导入jar包

    1.首先在idea左上角找到" File ",然后找到 "Project structure" 2.接着选择 " java ",选择后接着会 ...

  8. Linux root目录空间过小,加大空间

    1. 查看还有多少空间可以使用: df -h 这里可以看出来home的空间还很大,可以分配给root 2. 扩容根目录的思路如下: 将/home文件夹备份,删除/home文件系统所在的逻辑卷,增大/文 ...

  9. canvas 元素覆盖&穿透问题

    给网站添加canvas动态背景.完后发现有a标签无法点击,想到是canvas覆盖了(但有些是可以的).网上查找,有解决穿透的问题,但canvas的鼠标事件会无效.后发现是定位问题. canvas样式 ...

  10. SpringBoot整合Elasticsearch7

    SpringBoot连接ElasticSearch有以下种方式, TransportClient,9300端口,在 7.x 中已经被弃用,据说在8.x 中将完全删除 restClient,9200端口 ...