Interview essential: CAS lock-free mechanism

The principle of the CAS lock-free mechanism, one of the high-frequency questions for interviews. In fact, the CAS lock-free mechanism is not used directly in daily development. It is used through a series of packaged tools.

Maybe the interviewer doesn't ask questions and doesn't even know that there is such a thing.

1. Can you talk about your understanding of CAS?

Reference answer:

Usually we mentioned three ways to ensure multi-thread safety. One is to use the Synchronize keyword, but there is a problem that multi-threading after using Synchronize to lock is equivalent to serial, and the execution efficiency is not too high, so In the high concurrency scenario, using the second method of Lock lock, Lock lock is greatly improved in performance than using the Synchronize keyword, in fact, the bottom of the Lock lock is achieved through the AQS + CAS mechanism; the third method is to use The Atomic "e Tao Mi Ke" atomic operation class under the Java concurrent package. After using the atomic class, it is not necessary to use the Synchronize keyword or Lock to lock. It is also thread-safe. The bottom layer of the atomic class is implemented based on the CAS lock-free algorithm. .

CAS is exchanged after comparison. CAS has three operands, the old value A, the new value B, and the memory value V that needs to be read. When updating a variable, CAS will modify the memory value V to B if and only if A = V is the same. Otherwise, do nothing.

The reference answer is a bit long-winded, mentioning the use of the atomic class in the CAS application scenario, but more to say there is something

2. Can you talk about the realization principle of CAS?

Reference answer

The CAS in Java is implemented by calling the native method of the Unsafe "Anseifu" class, and then C calls the CPU's underlying commands.

If you ask Unsafe?

The Unsafe class is a relatively low-level class provided by JDK. It is an unsafe class. The official does not recommend it to developers. ①Unsafe may be removed in the future, that is, the high version jdk may not run; ②Unsafe many methods must provide the memory address and the address of the object to be replaced, there will be some calculation problems in the middle, once the problem occurs will cause the jvm instance to crash ; ③ The method of direct access to memory provided in Unsafe is not managed by jvm and requires manual operation, if negligent may trigger a memory leak;

Unsafe is usually used to: " Additional supplement, just understand "

  • Memory management: including allocating memory and freeing memory
  • 操作类、对象、变量:通过获取对象和变量偏移量直接修改数据
  • 挂起与恢复:将线程阻塞或者恢复阻塞状态
  • CAS:调用 CPU 的 CAS 指令进行比较和交换
  • 内存屏障:定义内存屏障,避免指令重排序

如果你想了解加深CAS原理,那么可以敲出一个原子类变量,比如AtomicInteger,直接看一下他的源码,找一下Unsafe是怎么大量应用的。

3、能说一下CAS的优缺点?

缺点

  1. CPU开销比较大
    在并发量比较高的情况下,如果线程反复尝试更新一个变量,却又一直更新不成功,循环往复,会给CPU带来很大的压力。
  2. 只能保证一个共享变量的原子操作
    只能是一个变量的原子操作,无法确保整个代码块的原子性,也就是在需要保证2个及以上变量共同进行原子性的更新时,就不得不使用Synchronized了
  3. ABA问题
    假设有一个变量 A ,经修改后变为B,然后又修改为 A,实际已经修改过了,但 CAS 可能无法感知,造成了不合理的值修改操作。
    解决方案是:使用版本号,在变量前面追加上版本号,每次变量更新的时候把版本号加一,那么A-B-A 就会变成1A-2B-3A

*优点

在一般情况下,性能优先于锁的使用。

4、最后补充

从思想上来说,Synchronized属于悲观锁,悲观地认为程序中的并发情况严重,所以严防死守。

CAS属于乐观锁,乐观地认为程序中的并发情况不那么严重,所以让线程不断去尝试更新。

CAS和Synchronized没有绝对的好与坏,关键看使用场景。在并发量非常高的情况下,反而用同步锁更合适一些。

 

Guess you like

Origin www.cnblogs.com/niceyoo/p/12683341.html
Recommended