自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從JUC源碼看CAS,我做了個(gè)筆記 ......

開(kāi)發(fā) 前端
" JUC包下大量使用了CAS,工作和面試中也經(jīng)常遇到CAS,包括說(shuō)到樂(lè)觀鎖,也不可避免的想起CAS,那CAS究竟是什么? "

[[341019]]

前言

" JUC包下大量使用了CAS,工作和面試中也經(jīng)常遇到CAS,包括說(shuō)到樂(lè)觀鎖,也不可避免的想起CAS,那CAS究竟是什么? "

1.什么是CAS?

說(shuō)到CAS,基本上都會(huì)想到樂(lè)觀鎖、AtomicInteger、Unsafe ...

當(dāng)然也有可能啥也沒(méi)想到!

 

不管你們?cè)趺聪耄?我第一印象是樂(lè)觀鎖,畢竟做交易更新交易狀態(tài)經(jīng)常用到樂(lè)觀鎖,就自然想到這個(gè)SQL:

  1. update trans_order  
  2. set order_status = 1  
  3. where order_no = 'xxxxxxxxxxx' and order_status = 0; 

其實(shí)就是 set和where里面都攜帶order_status。

那什么是CAS?

CAS就是Compare-and-Swap,即比較并替換,在并發(fā)算法時(shí)常用,并且在JUC(java.util.concurrent)包下很多類都使用了CAS。

非常常見(jiàn)的問(wèn)題就是多線程操作i++問(wèn)題。一般解決辦法就是添加 synchronized 關(guān)鍵字修飾,當(dāng)然也可以使用 AtomicInteger 代碼舉例如下:

  1. public class CasTest { 
  2.  
  3.     private static final CountDownLatch LATCH = new CountDownLatch(10); 
  4.  
  5.     private static int NUM_I = 0; 
  6.     private static volatile int NUM_J = 0; 
  7.     private static final AtomicInteger NUM_K = new AtomicInteger(0); 
  8.  
  9.     public static void main(String[] args) throws InterruptedException { 
  10.  
  11.         ExecutorService threadPool = Executors.newFixedThreadPool(10); 
  12.         for (int i = 0; i < 10; i++) { 
  13.  
  14.             threadPool.execute(new Runnable() { 
  15.                 public void run() { 
  16.                     for (int j = 0; j < 10000; j++) { 
  17.                         NUM_I++; 
  18.                         NUM_J++; 
  19.                         NUM_K.incrementAndGet(); 
  20.                     } 
  21.                     LATCH.countDown(); 
  22.                 } 
  23.             }); 
  24.         } 
  25.         LATCH.await(); 
  26.  
  27.         System.out.println("NUM_I = " + NUM_I); 
  28.         System.out.println("NUM_J = " + NUM_J); 
  29.         System.out.println("NUM_K = " + NUM_K.get()); 
  30.         threadPool.shutdown(); 
  31.     } 
  32.  

下面就從AtomicInteger開(kāi)始了解CAS。

2.源碼分析

  1. public class AtomicInteger extends Number implements java.io.Serializable { 
  2.     private static final long serialVersionUID = 6214790243416807050L; 
  3.  
  4.     // setup to use Unsafe.compareAndSwapInt for updates 
  5.     private static final Unsafe unsafe = Unsafe.getUnsafe(); 
  6.     private static final long valueOffset; 
  7.  
  8.     static { 
  9.         try { 
  10.             valueOffset = unsafe.objectFieldOffset 
  11.                 (AtomicInteger.class.getDeclaredField("value")); 
  12.         } catch (Exception ex) { throw new Error(ex); } 
  13.     } 
  14.  
  15.     private volatile int value; 
  16.  
  17.     public final int incrementAndGet() { 
  18.         return unsafe.getAndAddInt(this, valueOffset, 1) + 1; 
  19.     } 
  20.     public final int decrementAndGet() { 
  21.         return unsafe.getAndAddInt(this, valueOffset, -1) - 1; 
  22.     } 
  23.  

可以看出里面使用了Unsafe類下的getAndAddInt方法,Unsafe類很多方法是本地(native)方法,主要是硬件級(jí)別的原子操作。

  1. /** 
  2.  * @param var1 當(dāng)前對(duì)象 
  3.  * @param var2 當(dāng)前對(duì)象在內(nèi)存偏移量,Unsafe可以根據(jù)內(nèi)存偏移地址獲取數(shù)據(jù) 
  4.  * @param var4 操作值 
  5.  * @return 
  6.  */ 
  7. public final int getAndAddInt(Object var1, long var2, int var4) { 
  8.     int var5; 
  9.     do { 
  10.         // 獲取在var1在內(nèi)存的值 
  11.         var5 = this.getIntVolatile(var1, var2); 
  12.         // 將var1賦值為var5+var4, 賦值時(shí)會(huì)判斷var1是否為var5 
  13.     } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); 
  14.  
  15.     return var5; 
  16. // 原子操作 
  17. public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5); 

至于 compareAndSwapInt 的分析就忽略了。

看完代碼過(guò)程其實(shí)就是:

  •  比較var1的值是否為var4,是的話將var1更新為var5。
  • 如果不是的話就一直循環(huán),直到var1是var4。

 

3.問(wèn)題總結(jié)

 

  • 這要是一直獲取不到,豈不是一直循環(huán)。線程多的情況下,會(huì)自旋很長(zhǎng)時(shí)間,導(dǎo)致浪費(fèi)資源。
  • 你更新了, 我又給你更新回去了,你也不知道。ABA問(wèn)題!比如像這樣,A想更新值為a,還未搶到資源,這時(shí)候B進(jìn)行了更新,將對(duì)象更新為了b,然后又馬上更新回了a, 這時(shí)候A是什么都不知道的。

以樂(lè)觀鎖舉例:

  1. -- 0 -> 1 
  2. update trans_order  
  3. set order_status = 1  
  4. where order_no = 'xxxxxxxxxxx' and order_status = 0; 
  5.  
  6. -- 1 -> 0 
  7. update trans_order  
  8. set order_status = 1  
  9. where order_no = 'xxxxxxxxxxx' and order_status = 0; 
  10.  
  11. -- 0 -> 1 
  12. update trans_order  
  13. set order_status = 1  
  14. where order_no = 'xxxxxxxxxxx' and order_status = 0; 

解決辦法可以添加version進(jìn)行版本號(hào)控制。

  1. -- 0 -> 1 
  2. update trans_order  
  3. set order_status = 1  
  4. where order_no = 'xxxxxxxxxxx' and order_status = 0 and version = 0; 
  5.  
  6. -- 1 -> 0 
  7. update trans_order  
  8. set order_status = 1  
  9. where order_no = 'xxxxxxxxxxx' and order_status = 0 and version = 1; 
  10.  
  11. -- 0 -> 1 
  12. update trans_order  
  13. set order_status = 1  
  14. where order_no = 'xxxxxxxxxxx' and order_status = 0 and version = 0; 

代碼中可以看 AtomicStampedReference 類:

  1. /** 
  2.  * 以原子方式設(shè)置該引用和標(biāo)志給定的更新值的值, 
  3.  * 如果當(dāng)前引用==預(yù)期的引用,并且當(dāng)前標(biāo)志==預(yù)期標(biāo)志。 
  4.  * 
  5.  * @param expectedReference 預(yù)期引用 
  6.  * @param newReference 更新的值 
  7.  * @param expectedStamp 預(yù)期標(biāo)志 
  8.  * @param newStamp 更新的標(biāo)志 
  9.  * @return {@code true} if successful 
  10.  */ 
  11. public boolean compareAndSet(V   expectedReference, 
  12.                              V   newReference, 
  13.                              int expectedStamp, 
  14.                              int newStamp) { 
  15.     Pair<V> current = pair; 
  16.     return 
  17.         expectedReference == current.reference && 
  18.         expectedStamp == current.stamp && 
  19.         ((newReference == current.reference && 
  20.             newStamp == current.stamp) || 
  21.             casPair(current, Pair.of(newReference, newStamp))); 

其實(shí)就是額外增加一個(gè)標(biāo)志(stamp)來(lái)防止ABA的問(wèn)題, 類似樂(lè)觀鎖的version。

本文轉(zhuǎn)載自微信公眾號(hào)「劉志航」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系劉志航公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: 劉志航
相關(guān)推薦

2021-07-14 09:48:15

Linux源碼Epoll

2020-07-15 15:09:21

Python掃雷游戲Windows

2021-03-10 08:20:54

設(shè)計(jì)模式OkHttp

2020-06-14 15:09:00

JavaScript開(kāi)發(fā)技術(shù)

2018-02-02 15:48:47

ChromeDNS解析

2021-07-15 14:27:47

LinuxSocketClose

2017-04-05 20:00:32

ChromeObjectJS代碼

2020-10-10 07:00:16

LinuxSocketTCP

2021-06-10 09:52:33

LinuxTCPAccept

2020-09-23 12:32:18

網(wǎng)絡(luò)IOMySQL

2022-03-18 22:39:57

動(dòng)態(tài)內(nèi)存malloc

2022-02-22 20:35:22

公鑰私鑰數(shù)據(jù)

2025-03-06 13:10:32

2022-12-05 18:17:06

技術(shù)

2023-11-28 12:00:22

應(yīng)用程序API

2021-12-30 08:55:41

Log4j2FastJson漏洞

2017-02-09 15:15:54

Chrome瀏覽器

2017-02-28 10:05:56

Chrome源碼

2017-11-21 14:56:59

2023-03-13 07:43:51

PHP類型轉(zhuǎn)換
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)