今天就跟大家聊聊有關(guān)java中ReentrantLock的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的10年時(shí)間我們累計(jì)服務(wù)了上千家以及全國政企客戶,如柴油發(fā)電機(jī)等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致稱譽(yù)。
十載的金口河網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整金口河建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“金口河網(wǎng)站設(shè)計(jì)”,“金口河網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
ReentrantLock稱為重入鎖,比內(nèi)部鎖synchonized擁有更強(qiáng)大的功能,它可中斷、可定時(shí)、設(shè)置公平鎖
【注】使用ReentrantLock時(shí),一定要釋放鎖,一般釋放放到finnal里寫。
提供以下重要的方法
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等待,等待過程被打斷。打斷后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)
看完上述內(nèi)容,你們對(duì)java中ReentrantLock的作用是什么有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。