前言java
Java語言中有許多原生線程安全的數據結構,好比ArrayBlockingQueue、CopyOnWriteArrayList、LinkedBlockingQueue,它們線程安全的實現方式並不是經過synchronized關鍵字,而是經過java.util.concurrent.locks.ReentrantLock來實現。程序員
鎖的底層實現面試
不管什麼語言在操做系統層面鎖的操做都會變成系統調用(System Call),以 Linux 爲例,就是 futex 函數,能夠把它理解爲兩個函數:futex_wait(s),對變量 s 加鎖;futex_wake(s)釋放 s 上的鎖,喚醒其餘線程。安全
在ReentrantLock中很明顯能夠看到其中同步包括兩種,分別是公平的FairSync和非公平的NonfairSync。數據結構
公平鎖的做用就是嚴格按照線程啓動的順序來執行的,不容許其餘線程插隊執行的;而非公平鎖是容許插隊的。app
默認狀況下ReentrantLock是經過非公平鎖來進行同步的,包括synchronized關鍵字都是如此,由於這樣性能會更好。ide
由於從線程進入了RUNNABLE狀態,能夠執行開始,到實際線程執行是要比較久的時間的。函數
並且,在一個鎖釋放以後,其餘的線程會須要從新來獲取鎖。其中經歷了持有鎖的線程釋放鎖,其餘線程從掛起恢復到RUNNABLE狀態,其餘線程請求鎖,得到鎖,線程執行,這一系列步驟。若是這個時候,存在一個線程直接請求鎖,可能就避開掛起到恢復RUNNABLE狀態的這段消耗,因此性能更優化。性能
/** * Creates an instance of {@code ReentrantLock}. * This is equivalent to using {@code ReentrantLock(false)}. */ public ReentrantLock() { sync = new NonfairSync(); }
默認狀態,使用的ReentrantLock()就是非公平鎖。再參考以下代碼,咱們知道ReentrantLock的獲取鎖的操做是經過裝飾模式代理給sync的。優化
/** * Acquires the lock. * * <p>Acquires the lock if it is not held by another thread and returns * immediately, setting the lock hold count to one. * * <p>If the current thread already holds the lock then the hold * count is incremented by one and the method returns immediately. * * <p>If the lock is held by another thread then the * current thread becomes disabled for thread scheduling * purposes and lies dormant until the lock has been acquired, * at which time the lock hold count is set to one. */ public void lock() { sync.lock(); }
下面參考一下FairSync和NonfairSync對lock方法的實現:
/** * Sync object for non-fair locks */ static final class NonfairSync extends Sync { /** * Performs lock. Try immediate barge, backing up to normal * acquire on failure. */ final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } } /** * Sync object for fair locks */ static final class FairSync extends Sync { final void lock() { acquire(1); } }
當使用非公平鎖的時候,會馬上嘗試配置狀態,成功了就會插隊執行,失敗了就會和公平鎖的機制同樣,調用acquire()方法,以排他的方式來獲取鎖,成功了馬上返回,不然將線程加入隊列,知道成功調用爲止。歡迎你們關注個人公種浩【程序員追風】,2019年多家公司java面試題整理了1000多道400多頁pdf文檔,文章都會在裏面更新,整理的資料也會放在裏面。
總結
上鎖的過程自己也是有時間開銷的,若是操做資源的時間比上鎖的時間還短建議使用非公平鎖能夠提升系統的吞吐率;不然就老老實實的用公平鎖。
最後
歡迎你們一塊兒交流,喜歡文章記得點個贊喲,感謝支持!