abstrak:一、CAS和synchronized適用場景1、對于資源競爭較少的情況,使用synchronized同步鎖進行線程阻塞和喚醒切換以及用戶態(tài)內核態(tài)間的切換操作額外浪費消耗cpu資源;而CAS基于硬件實現(xiàn),不需要進入內核,不需要切換線程,操作自旋幾率較少,因此可以獲得更高的性能。2、對于資源競爭嚴重的情況,CAS自旋的概率會比較大,從而浪費更多的CPU資源,效率低于synchronized。以java
一、CAS和synchronized適用場景
1、對于資源競爭較少的情況,使用synchronized同步鎖進行線程阻塞和喚醒切換以及用戶態(tài)內核態(tài)間的切換操作額外浪費消耗cpu資源;而CAS基于硬件實現(xiàn),不需要進入內核,不需要切換線程,操作自旋幾率較少,因此可以獲得更高的性能。
2、對于資源競爭嚴重的情況,CAS自旋的概率會比較大,從而浪費更多的CPU資源,效率低于synchronized。以java.util.concurrent.atomic包中AtomicInteger類為例,其getAndIncrement()方法實現(xiàn)如下:
public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } }
如果compareAndSet(current, next)方法成功執(zhí)行,則直接返回;如果線程競爭激烈,導致compareAndSet(current, next)方法一直不能成功執(zhí)行,則會一直循環(huán)等待,直到耗盡cpu分配給該線程的時間片,從而大幅降低效率。
二、CAS錯誤的使用場景
public class CASDemo { private final int THREAD_NUM = 1000; private final int MAX_VALUE = 20000000; private AtomicInteger casI = new AtomicInteger(0); private int syncI = 0; private String path = "/Users/pingping/DataCenter/Books/Linux/Linux常用命令詳解.txt"; public void casAdd() throws InterruptedException { long begin = System.currentTimeMillis(); Thread[] threads = new Thread[THREAD_NUM]; for (int i = 0; i < THREAD_NUM; i++) { threads[i] = new Thread(new Runnable() { public void run() { while (casI.get() < MAX_VALUE) { casI.getAndIncrement(); } } }); threads[i].start(); } for (int j = 0; j < THREAD_NUM; j++) { threads[j].join(); } System.out.println("CAS costs time: " + (System.currentTimeMillis() - begin)); } public void syncAdd() throws InterruptedException { long begin = System.currentTimeMillis(); Thread[] threads = new Thread[THREAD_NUM]; for (int i = 0; i < THREAD_NUM; i++) { threads[i] = new Thread(new Runnable() { public void run() { while (syncI < MAX_VALUE) { synchronized ("syncI") { ++syncI; } } } }); threads[i].start(); } for (int j = 0; j < THREAD_NUM; j++) threads[j].join(); System.out.println("sync costs time: " + (System.currentTimeMillis() - begin)); } }
在我的雙核cpu上運行,結果如下:
可見在不同的線程下,采用CAS計算消耗的時間遠多于使用synchronized方式。原因在于第15行
14 while (casI.get() < MAX_VALUE) {
15 casI.getAndIncrement();
16 }
的操作是一個耗時非常少的操作,15行執(zhí)行完之后會立刻進入循環(huán),繼續(xù)執(zhí)行,從而導致線程沖突嚴重。
三、改進的CAS使用場景
為了解決上述問題,只需要讓每一次循環(huán)執(zhí)行的時間變長,即可以大幅減少線程沖突。修改代碼如下:
public class CASDemo { private final int THREAD_NUM = 1000; private final int MAX_VALUE = 1000; private AtomicInteger casI = new AtomicInteger(0); private int syncI = 0; private String path = "/Users/pingping/DataCenter/Books/Linux/Linux常用命令詳解.txt"; public void casAdd2() throws InterruptedException { long begin = System.currentTimeMillis(); Thread[] threads = new Thread[THREAD_NUM]; for (int i = 0; i < THREAD_NUM; i++) { threads[i] = new Thread(new Runnable() { public void run() { while (casI.get() < MAX_VALUE) { casI.getAndIncrement(); try (InputStream in = new FileInputStream(new File(path))) { while (in.read() != -1); } catch (IOException e) { e.printStackTrace(); } } } }); threads[i].start(); } for (int j = 0; j < THREAD_NUM; j++) threads[j].join(); System.out.println("CAS Random costs time: " + (System.currentTimeMillis() - begin)); } public void syncAdd2() throws InterruptedException { long begin = System.currentTimeMillis(); Thread[] threads = new Thread[THREAD_NUM]; for (int i = 0; i < THREAD_NUM; i++) { threads[i] = new Thread(new Runnable() { public void run() { while (syncI < MAX_VALUE) { synchronized ("syncI") { ++syncI; } try (InputStream in = new FileInputStream(new File(path))) { while (in.read() != -1); } catch (IOException e) { e.printStackTrace(); } } } }); threads[i].start(); } for (int j = 0; j < THREAD_NUM; j++) threads[j].join(); System.out.println("sync costs time: " + (System.currentTimeMillis() - begin)); } }
在while循環(huán)中,增加了一個讀取文件內容的操作,該操作大概需要耗時40ms,從而可以減少線程沖突。測試結果如下:
可見在資源沖突比較小的情況下,采用CAS方式和synchronized同步效率差不多。為什么CAS相比synchronized沒有獲得更高的性能呢?
測試使用的jdk為1.7,而從jdk1.6開始,對鎖的實現(xiàn)引入了大量的優(yōu)化,如鎖粗化(Lock Coarsening)、鎖消除(Lock Elimination)、輕量級鎖(Lightweight Locking)、偏向鎖(Biased Locking)、適應性自旋(Adaptive Spinning)等技術來減少鎖操作的開銷。而其中自旋鎖的原理,類似于CAS自旋,甚至比CAS自旋更為優(yōu)化。
四、總結
1、使用CAS在線程沖突嚴重時,會大幅降低程序性能;CAS只適合于線程沖突較少的情況使用。
2、synchronized在jdk1.6之后,已經(jīng)改進優(yōu)化。synchronized的底層實現(xiàn)主要依靠Lock-Free的隊列,基本思路是自旋后阻塞,競爭切換后繼續(xù)競爭鎖,稍微犧牲了公平性,但獲得了高吞吐量。在線程沖突較少的情況下,可以獲得和CAS類似的性能;而線程沖突嚴重的情況下,性能遠高于CAS。