Java鎖之可重入鎖介紹
鎖作為并發(fā)共享數(shù)據(jù),保證一致性的工具,在JAVA平臺(tái)有多種實(shí)現(xiàn)(如 synchronized 和 ReentrantLock等等 ) 。這些已經(jīng)寫(xiě)好提供的鎖為我們開(kāi)發(fā)提供了便利,但是鎖的具體性質(zhì)以及類(lèi)型卻很少被提及。本系列文章將分析JAVA下常見(jiàn)的鎖名稱(chēng)以及特性,為大家答疑解惑。
四、可重入鎖:
本文里面講的是廣義上的可重入鎖,而不是單指JAVA下的ReentrantLock。
可重入鎖,也叫做遞歸鎖,指的是同一線程 外層函數(shù)獲得鎖之后 ,內(nèi)層遞歸函數(shù)仍然有獲取該鎖的代碼,但不受影響。
在JAVA環(huán)境下 ReentrantLock 和synchronized 都是 可重入鎖。
下面是使用實(shí)例:
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();
}
}
兩個(gè)例子最后的結(jié)果都是正確的,即 同一個(gè)線程id被連續(xù)輸出兩次。
結(jié)果如下:
Threadid: 8
Threadid: 8
Threadid: 10
Threadid: 10
Threadid: 9
Threadid: 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);
}
}
對(duì)于自旋鎖來(lái)說(shuō):
1、若有同一線程兩調(diào)用lock() ,會(huì)導(dǎo)致第二次調(diào)用lock位置進(jìn)行自旋,產(chǎn)生了死鎖
說(shuō)明這個(gè)鎖并不是可重入的。(在lock函數(shù)內(nèi),應(yīng)驗(yàn)證線程是否為已經(jīng)獲得鎖的線程)
2、若1問(wèn)題已經(jīng)解決,當(dāng)unlock()第一次調(diào)用時(shí),就已經(jīng)將鎖釋放了。實(shí)際上不應(yīng)釋放鎖。
(采用計(jì)數(shù)次進(jìn)行統(tǒng)計(jì))
修改之后,如下:
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);
}
}
}
}
該自旋鎖即為可重入鎖。
相關(guān)文章
Java中RSA加密解密的實(shí)現(xiàn)方法分析
這篇文章主要介紹了Java中RSA加密解密的實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了java實(shí)現(xiàn)RSA加密解密算法的具體步驟與相關(guān)操作技巧,并附帶了關(guān)于RSA算法密鑰長(zhǎng)度/密文長(zhǎng)度/明文長(zhǎng)度的參考說(shuō)明,需要的朋友可以參考下2017-07-07Java程序的初始化順序,static{}靜態(tài)代碼塊和實(shí)例語(yǔ)句塊的使用方式
這篇文章主要介紹了Java程序的初始化順序,static{}靜態(tài)代碼塊和實(shí)例語(yǔ)句塊的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01使用游長(zhǎng)編碼對(duì)字符串壓縮 Run Length編碼示例
這篇文章主要介紹了Run Length編碼的一個(gè)示例,大家參考使用吧2014-01-01Java 如何將表格數(shù)據(jù)導(dǎo)入word文檔中
這篇文章主要介紹了Java將表格數(shù)據(jù)導(dǎo)入word文檔中的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06BeanUtils.copyProperties()所有的空值不復(fù)制問(wèn)題
這篇文章主要介紹了BeanUtils.copyProperties()所有的空值不復(fù)制問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Spring Boot 如何解決富文本上傳圖片跨域問(wèn)題
這篇文章主要介紹了Spring Boot 如何解決富文本上傳圖片跨域問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解
這篇文章主要介紹了Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解,BeanFactory及其子類(lèi)是Spring IOC容器中最重要的一個(gè)類(lèi),BeanFactory由類(lèi)名可以看出其是一個(gè)Bean工廠類(lèi),其實(shí)它確實(shí)是一個(gè)Bean工廠類(lèi),完成Bean的初始化操作,需要的朋友可以參考下2023-12-12基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯(cuò)點(diǎn)、幻讀
這篇文章主要介紹了基于Spring中的事務(wù)@Transactional細(xì)節(jié)與易錯(cuò)點(diǎn)、幻讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11