java ReentrantLock詳解
介紹
ReentrantLock稱(chēng)為重入鎖,比內(nèi)部鎖synchonized擁有更強(qiáng)大的功能,它可中斷、可定時(shí)、設(shè)置公平鎖
【注】使用ReentrantLock時(shí),一定要釋放鎖,一般釋放放到finnal里寫(xiě)。
提供以下重要的方法
- lock():獲得鎖,如果鎖已被占用,則等待
- lockInterruptibly():獲得鎖,但有限響應(yīng)中斷
- unlock():釋放鎖
- tryLock():嘗試獲取鎖。如果獲得,返回true;否則返回false
- tryLock(long time, TimeUnit unit):在給定時(shí)間內(nèi)獲得鎖。如果獲得返回true;否則返回false
示例
例子1
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockTest {
ReentrantLock lock;
ReentrantLockTest(ReentrantLock lock) {
this.lock = lock;
}
private Runnable getRunnable() {
return new Runnable() {
@Override
public void run() {
while(true) {
try {
if (lock.tryLock()) {
try {
System.out.println("Locked:" + Thread.currentThread().getName());
Thread.sleep(800);
} finally {
lock.unlock();
System.out.println("UnLocked:" + Thread.currentThread().getName());
}
System.out.println("break before");
break;
} else {
//System.out.println("Unable to lock " + Thread.currentThread().getName());
}
} catch (InterruptedException e){
System.out.println(Thread.currentThread() + " is Interupted");
e.printStackTrace();
}
}
}
};
}
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
ReentrantLockTest test = new ReentrantLockTest(lock);
ReentrantLockTest test2 = new ReentrantLockTest(lock);
Thread thread1 = new Thread(test.getRunnable(), "firstThread");
Thread thread2 = new Thread(test2.getRunnable(), "secondThread");
thread1.start();
thread2.start();
try {
Thread.sleep(300);
}catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("interupt begin");
thread2.interrupt();
System.out.println("interupt end");
}
}
一次執(zhí)行結(jié)果:
Locked:firstThread
interupt begin
interupt end
UnLocked:firstThread
break before
Locked:secondThread
UnLocked:secondThread
Thread[secondThread,5,main] is Interupted
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.jihite.templet.JavaBase.ReentrantLockTest$1.run(ReentrantLockTest.java:23)
at java.lang.Thread.run(Thread.java:748)
Locked:secondThread
UnLocked:secondThread
break before
分析:firstThread執(zhí)行,secondThread不停的判斷是否可以獲得鎖,當(dāng)firstThread執(zhí)行完,secondThread執(zhí)行后被打斷
例子2
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockTest {
ReentrantLock lock;
ReentrantLockTest(ReentrantLock lock) {
this.lock = lock;
}
private Runnable getRunnable() {
return new Runnable() {
@Override
public void run() {
while(true) {
try {
if (lock.tryLock(700, TimeUnit.MILLISECONDS)) {
try {
System.out.println("Locked:" + Thread.currentThread().getName());
Thread.sleep(800);
} finally {
lock.unlock();
System.out.println("UnLocked:" + Thread.currentThread().getName());
}
System.out.println("break before");
break;
} else {
//System.out.println("Unable to lock " + Thread.currentThread().getName());
}
} catch (InterruptedException e){
System.out.println(Thread.currentThread() + " is Interupted");
e.printStackTrace();
}
}
}
};
}
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
ReentrantLockTest test = new ReentrantLockTest(lock);
ReentrantLockTest test2 = new ReentrantLockTest(lock);
Thread thread1 = new Thread(test.getRunnable(), "firstThread");
Thread thread2 = new Thread(test2.getRunnable(), "secondThread");
thread1.start();
thread2.start();
try {
Thread.sleep(300);
}catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("interupt begin");
thread2.interrupt();
System.out.println("interupt end");
}
}
一次執(zhí)行結(jié)果
Locked:firstThread
interupt begin
interupt end
Thread[secondThread,5,main] is Interupted
java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireNanos(AbstractQueuedSynchronizer.java:936)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireNanos(AbstractQueuedSynchronizer.java:1247)
at java.util.concurrent.locks.ReentrantLock.tryLock(ReentrantLock.java:442)
at com.jihite.templet.JavaBase.ReentrantLockTest$1.run(ReentrantLockTest.java:19)
at java.lang.Thread.run(Thread.java:748)
Locked:secondThread
UnLocked:firstThread
break before
UnLocked:secondThread
break before
分析:firstThread執(zhí)行,secondThread等待,等待過(guò)程被打斷。打斷后firstThread執(zhí)行結(jié)束了,secondThread得到鎖,繼續(xù)執(zhí)行
例子3
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockTest2 {
ReentrantLock lock;
ReentrantLockTest2(ReentrantLock lock) {
this.lock = lock;
}
private Runnable getRunnable() {
return new Runnable() {
@Override
public void run() {
while (true) {
try {
try {
lock.lock();
// lock.lockInterruptibly();
System.out.println("Locked:" + Thread.currentThread().getName());
Thread.sleep(800);
break;
} finally {
lock.unlock();
System.out.println("UnLocked:" + Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
ReentrantLockTest2 test = new ReentrantLockTest2(lock);
ReentrantLockTest2 test2 = new ReentrantLockTest2(lock);
Thread thread1 = new Thread(test.getRunnable(), "firstThread");
Thread thread2 = new Thread(test2.getRunnable(), "secondThread");
thread1.start();
thread2.start();
try {
Thread.sleep(600);
}catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("interupt begin");
thread2.interrupt();
System.out.println("interupt end");
}
}
一次執(zhí)行結(jié)果
Locked:firstThread
interupt begin
interupt end
UnLocked:firstThread
Locked:secondThread
UnLocked:secondThread
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.jihite.templet.JavaBase.ReentrantLockTest2$1.run(ReentrantLockTest2.java:22)
at java.lang.Thread.run(Thread.java:748)
Locked:secondThread
UnLocked:secondThread
分析:firstThread先獲得鎖執(zhí)行,secondThread在等待,此時(shí)中斷并未打斷等待。firstThread執(zhí)行完,secondThread獲取后被打斷
例子4
public class ReentrantLockTest2 {
ReentrantLock lock;
ReentrantLockTest2(ReentrantLock lock) {
this.lock = lock;
}
private Runnable getRunnable() {
return new Runnable() {
@Override
public void run() {
while (true) {
try {
try {
// lock.lock();
lock.lockInterruptibly();
System.out.println("Locked:" + Thread.currentThread().getName());
Thread.sleep(800);
break;
} finally {
lock.unlock();
System.out.println("UnLocked:" + Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
ReentrantLockTest2 test = new ReentrantLockTest2(lock);
ReentrantLockTest2 test2 = new ReentrantLockTest2(lock);
Thread thread1 = new Thread(test.getRunnable(), "firstThread");
Thread thread2 = new Thread(test2.getRunnable(), "secondThread");
thread1.start();
thread2.start();
try {
Thread.sleep(600);
}catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("interupt begin");
thread2.interrupt();
System.out.println("interupt end");
}
}
一次執(zhí)行結(jié)果
Locked:firstThread
interupt begin
interupt end
Exception in thread "secondThread" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
at com.jihite.templet.JavaBase.ReentrantLockTest2$1.run(ReentrantLockTest2.java:25)
at java.lang.Thread.run(Thread.java:748)
分析:lock.lockInterruptibly();在執(zhí)行過(guò)程中可以響應(yīng)中斷時(shí)間
以上所述是小編給大家介紹的java ReentrantLock詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java使用agent實(shí)現(xiàn)main方法之前的實(shí)例詳解
這篇文章主要介紹了Java使用agent實(shí)現(xiàn)main方法之前的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10
使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫(kù)數(shù)據(jù)同步
這篇文章主要介紹了使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫(kù)數(shù)據(jù)同步問(wèn)題,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06
Java類(lèi)加載策略之雙親委派機(jī)制全面分析講解
這篇文章主要介紹了Java雙親委派機(jī)制,雙親委派模型是Java加載類(lèi)的機(jī)制,采用雙親委派模型的好處是Java類(lèi)隨著它的類(lèi)加載器一起具備了一種帶有優(yōu)先級(jí)的層級(jí)關(guān)系,通過(guò)這種層級(jí)關(guān)系可以避免類(lèi)的重復(fù)加載,感興趣的朋友可以參考下2023-12-12
Java鍵值對(duì)Pair的使用方式和操作實(shí)現(xiàn)
鍵值對(duì)是一種常見(jiàn)的數(shù)據(jù)結(jié)構(gòu),它由一個(gè)唯一的鍵和與之關(guān)聯(lián)的值組成,本文就來(lái)介紹一下Java鍵值對(duì)Pair的使用方式和操作實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
JFileChooser實(shí)現(xiàn)對(duì)選定文件夾內(nèi)圖片自動(dòng)播放和暫停播放實(shí)例代碼
這篇文章主要介紹了JFileChooser實(shí)現(xiàn)對(duì)選定文件夾內(nèi)圖片自動(dòng)播放和暫停播放實(shí)例代碼,需要的朋友可以參考下2017-04-04
Java設(shè)計(jì)模式之單一職責(zé)原則精解
設(shè)計(jì)模式(Design pattern)代表了最佳的實(shí)踐,通常被有經(jīng)驗(yàn)的面向?qū)ο蟮能浖_(kāi)發(fā)人員所采用。設(shè)計(jì)模式是軟件開(kāi)發(fā)人員在軟件開(kāi)發(fā)過(guò)程中面臨的一般問(wèn)題的解決方案。本篇介紹設(shè)計(jì)模式七大原則之一的單一職責(zé)原則2022-02-02

