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. android下vulkan与opengles纹理互通

    先放demo源码地址:https://github.com/xxxzhou/aoce 06_mediaplayer 效果图: 主要几个点: 用ffmpeg打开rtmp流. 使用vulkan Compu ...

  2. PHP代码审计入门(SQL注入漏洞挖掘基础)

    SQL注入漏洞 SQL注入经常出现在登陆页面.和获取HTTP头(user-agent/client-ip等).订单处理等地方,因为这几个地方是业务相对复杂的,登陆页面的注入现在来说大多数是发生在HTT ...

  3. DC靶机1-9合集

    DC1 文章前提概述 本文介绍DC-1靶机的渗透测试流程 涉及知识点(比较基础): nmap扫描网段端口服务 msf的漏洞搜索 drupal7的命令执行利用 netcat反向shell mysql的基 ...

  4. [NOIP2013][LGOJ P1967]货车运输

    Problem Link 题目描述 A国有n座城市,编号从1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重 ...

  5. Mac 上超好用的代码对比工具 beyond compare,对比json差异

    导读 昨天下午,公司业务跑不通,然后开发组长让架构师联系我,给我发一个json和部署到dev上的微服务url,让我去测试下,将发来的json放到json.cn上愣是解析不出来,我就用之前的json请求 ...

  6. Java集合【3】-- iterable接口超级详细解析

    目录 iterable接口 1. 内部定义的方法 1.1 iterator()方法 1.2 forEach()方法 1.3 spliterator()方法 总结 iterable接口 整个接口框架关系 ...

  7. linux下安装python3.7.1

    一.安装依赖环境 输入命令:yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readlin ...

  8. SpringBoot 整合邮件oh-my-email 实现发送邮件功能

    导读 最近手头上要负责整个Message Gateway服务的搭建,涉及到:微信推送(点我直达).短信.邮件等等,到github上发现有个微型的开源邮件框架,整理下来,以备项目中使用到,到时候应该会使 ...

  9. 交换机三种端口模式Access、Hybrid和Trunk

    以太网端口有 3种链路类型:access.trunk.hybird 什么是链路类型? vlan的链路类型可以分为接入链路和干道链路. 1.接入链路(access link)指的交换机到用户设备的链路, ...

  10. golang 自学系列(四)——debug for vscode

    golang 自学系列(四)--(调试)VSCode For Debug 这里如何装 vscode 我就不说了 这里如何在 vscode 正常写代码我也不说了 在能正常用 vscode 写 go 语言 ...