可重新進入鎖 & 自旋鎖 & Java裡的AtomicReference和CAS操作 & Linux mutex不可重新進入

來源:互聯網
上載者:User

標籤:輸出   自旋鎖   可重新進入   核心   color   mutex   article   反向   return   

之前還是寫過蠻多的關於鎖的文章的:

http://www.cnblogs.com/charlesblc/p/5994162.html 《【轉載】Java中的鎖機制 synchronized & 偏向鎖 & 輕量級鎖 & 重量級鎖 & 各自》

http://www.cnblogs.com/charlesblc/p/5935326.html 《[Todo] 樂觀悲觀鎖,自旋互斥鎖等等》

http://www.cnblogs.com/charlesblc/p/6146917.html 《【轉載】同步和互斥的POSIX支援(互斥鎖,條件變數,自旋鎖)》

http://www.cnblogs.com/charlesblc/p/6134658.html 《【Todo】秒殺系統 & 樂觀鎖 & Nginx反向 Proxy》

http://www.cnblogs.com/charlesblc/p/5996255.html 《【Todo】【轉載】Java中的鎖機制2 - Lock》

 

再寫一個,加深理解吧。

 

在學習Java記憶體模型的時候,看到ReentrantLock的描述,有點記不清了,就查了一下資料。

參考如下這篇文章:http://www.jb51.net/article/57338.htm

鎖作為並發共用資料,保證一致性的工具,在JAVA平台有多種實現(如 synchronized 和 ReentrantLock等等 ) 。這些已經寫好提供的鎖為我們開發提供了便利,但是鎖的具體性質以及類型卻很少被提及。本系列文章將分析JAVA下常見的鎖名稱以及特性,為大家答疑解惑。

 

可重新進入鎖:

本文裡面講的是廣義上的可重新進入鎖,而不是單指JAVA下的ReentrantLock。

可重新進入鎖,也叫做遞迴鎖,指的是同一線程 外層函數獲得鎖之後 ,內層遞迴函式仍然有擷取該鎖的代碼,但不受影響。
在JAVA環境下 ReentrantLock 和synchronized 都是 可重新進入鎖。

使用執行個體:

public class Test implements Runnable{ public synchronized void get(){  System.out.println(Thread.currentThread().getId());  set(); } public synchronized void set(){  System.out.println(Thread.currentThread().getId()); } @Override public void run() {  get(); } public static void main(String[] args) {  Test ss=new Test();  new Thread(ss).start();  new Thread(ss).start();  new Thread(ss).start(); }}

結果如下,是正確的,即同一個線程id被連續輸出兩次。

Threadid: 8Threadid: 8Threadid: 10Threadid: 10Threadid: 9Threadid: 9

 

可重新進入鎖最大的作用是避免死結。
我們以自旋鎖作為例子。(註:自旋鎖,就是拿不到鎖的情況會不停自旋迴圈檢測來等待,不進入核心態沉睡,而是在使用者態自旋嘗試)

public class SpinLock { private AtomicReference<Thread> owner =new AtomicReference<>(); public void lock(){  Thread current = Thread.currentThread();  while(!owner.compareAndSet(null, current)){  } }
public void unlock (){ Thread current = Thread.currentThread(); owner.compareAndSet(current, null); }}

上面是自旋鎖的一種實現。

對於自旋鎖來說:

1、若有同一線程兩調用lock() ,會導致第二次調用lock位置進行自旋,產生了死結
說明這個鎖並不是可重新進入的。(在lock函數內,應驗證線程是否為已經獲得鎖的線程)
2、若1問題已經解決,當unlock()第一次調用時,就已經將鎖釋放了。實際上不應釋放鎖。
(採用計數次進行統計)

 

修改之後,如下:

public class SpinLock1 { private AtomicReference<Thread> owner =new AtomicReference<>(); private int count =0; public void lock(){  Thread current = Thread.currentThread();  if(current==owner.get()) {   count++;   return ;  }  while(!owner.compareAndSet(null, current)){  } }
public void unlock (){ Thread current = Thread.currentThread(); if(current==owner.get()){ if(count!=0){ count--; }else{ owner.compareAndSet(current, null); } } }}

這種方式實現的自旋鎖即為可重新進入鎖。

 

另,看一下mutex的情況:

Mutex可以分為遞迴鎖(recursive mutex)和非遞迴鎖(non-recursive mutex)。可遞迴鎖也可稱為可重新進入鎖(reentrant mutex),
非遞迴鎖又叫不可重新進入鎖(non-reentrant mutex)。

二者唯一的區別是,同一個線程可以多次擷取同一個遞迴鎖,不會產生死結。而如果一個線程多次擷取同一個非遞迴鎖,則會產生死結。

Windows下的Mutex和Critical Section是可遞迴的。

Linux下的pthread_mutex_t鎖預設是非遞迴的。可以顯示的設定PTHREAD_MUTEX_RECURSIVE屬性,將pthread_mutex_t設為遞迴鎖。

 

(完)

可重新進入鎖 & 自旋鎖 & Java裡的AtomicReference和CAS操作 & Linux mutex不可重新進入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.