日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

JUC并发编程八 并发架构--ReentrantLock

發(fā)布時(shí)間:2025/6/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JUC并发编程八 并发架构--ReentrantLock 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

相比synchronized,ReentrantLock具備以下特點(diǎn):

  • 可中斷
  • 可以設(shè)置超長(zhǎng)時(shí)間
  • 可以設(shè)置為公平鎖
  • 可以支持多個(gè)條件變量
    與synchronized一樣,都支持可重入.
  • 驗(yàn)證可重入

    import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantLock;@Slf4j(topic = "c.TestReentrantLock") public class TestReentrantLock {private static ReentrantLock lock = new ReentrantLock();public static void main(String[] args) {lock.lock();try{log.debug("進(jìn)入主方法");m1();}finally {lock.unlock();}}// 測(cè)試可重入public static void m1() {lock.lock();try{log.debug("進(jìn)入m1方法");m2();}finally {lock.unlock();}}public static void m2() {lock.lock();try{log.debug("進(jìn)入m2方法");}finally {lock.unlock();}} }

    驗(yàn)證可打斷性

    lock()方法不具備打斷性

    import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantLock;@Slf4j(topic = "c.TestReentrantLock") public class TestReentrantLock {private static ReentrantLock lock = new ReentrantLock();public static void main(String[] args) {// 防止無限制的等待下去Thread t1 = new Thread(()->{lock.lock();//lock()方法不能被打斷try{log.debug("獲取鎖");}finally {lock.unlock();log.debug("finally...");}});lock.lock();t1.start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}// 去打斷t1,不起作用。因?yàn)閘ock()方法不能被打斷t1.interrupt();}}

    locklockInterruptibly方法可以被打斷,停止線程無限制的等待.

    import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantLock;@Slf4j(topic = "c.TestReentrantLock") public class TestReentrantLock {private static ReentrantLock lock = new ReentrantLock();public static void main(String[] args) {// 防止無限制的等待下去Thread t1 = new Thread(()->{try{log.debug("嘗試獲取鎖");lock.lockInterruptibly();//locklockInterruptibly方法可以被打斷}catch (InterruptedException e){e.printStackTrace();log.debug("沒有或得鎖, 返回");return;}try{log.debug("獲取鎖");}finally {lock.unlock(); // 釋放鎖log.debug("finally...");}});lock.lock();t1.start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}log.debug("打斷t1");// 去打斷t1,不起作用。因?yàn)閘ock()方法不能被打斷t1.interrupt();} }

    驗(yàn)證鎖超時(shí)

    import lombok.extern.slf4j.Slf4j;import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock;@Slf4j(topic = "c.TestReentrantLock") public class TestReentrantLock {private static ReentrantLock lock = new ReentrantLock();public static void main(String[] args) {Thread t = new Thread(()->{// if(!lock.tryLock()){ // log.debug("獲取鎖失敗"); // return; // }try {// 嘗試獲取鎖,如果1秒內(nèi),獲取不到鎖就會(huì)獲取鎖失敗if(!lock.tryLock(1,TimeUnit.SECONDS)){ log.debug("獲取鎖失敗");return;}} catch (InterruptedException e) {// 表示可以調(diào)用 interrupt()方法,打斷線程log.debug("獲取鎖失敗");e.printStackTrace();return;}try{log.debug("獲取鎖成功");}finally {lock.unlock();}},"t1");lock.lock();log.debug("獲取鎖");t.start();try {Thread.sleep(2000); // 睡眠2秒} catch (InterruptedException e) {e.printStackTrace();}log.debug("釋放鎖");lock.unlock();} }

    驗(yàn)證條件變量

    import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock;@Slf4j(topic = "c.TestCondition") public class TestCondition {static boolean hasCigarette = false;static boolean hasTakeout = false;static ReentrantLock ROOM = new ReentrantLock();static Condition waitCigaretteSet = ROOM.newCondition();static Condition waitTakeoutSet = ROOM.newCondition();public static void main(String[] args) {new Thread(()->{ROOM.lock();try{log.debug("煙送到?jīng)]?[{}]",hasCigarette);while(!hasCigarette){log.debug("沒煙,先歇會(huì)...");try {waitCigaretteSet.await();} catch (InterruptedException e) {e.printStackTrace();}}log.debug("開始干活...");}finally {ROOM.unlock();}},"小南").start();new Thread(()->{ROOM.lock();try{log.debug("外賣送到?jīng)]?[{}]",hasTakeout);while(!hasTakeout){log.debug("沒外賣,先歇會(huì)...");try {waitTakeoutSet.await();} catch (InterruptedException e) {e.printStackTrace();}}log.debug("開始干活...");}finally {ROOM.unlock();}},"小女").start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}new Thread(()->{ROOM.lock();try{hasCigarette = true;waitCigaretteSet.signal();}finally {ROOM.unlock();}}).start();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}new Thread(()->{ROOM.lock();try{hasTakeout = true;waitTakeoutSet.signal();}finally {ROOM.unlock();}}).start();} }

    總結(jié)

    以上是生活随笔為你收集整理的JUC并发编程八 并发架构--ReentrantLock的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。