JUC并发编程八 并发架构--ReentrantLock
生活随笔
收集整理的這篇文章主要介紹了
JUC并发编程八 并发架构--ReentrantLock
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
相比synchronized,ReentrantLock具備以下特點:
與synchronized一樣,都支持可重入.
驗證可重入
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();}}// 測試可重入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();}} }驗證可打斷性
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,不起作用。因為lock()方法不能被打斷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,不起作用。因為lock()方法不能被打斷t1.interrupt();} }驗證鎖超時
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),獲取不到鎖就會獲取鎖失敗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();} }驗證條件變量
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("沒煙,先歇會...");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("沒外賣,先歇會...");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的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常考数据结构与算法: NC19 连续子数
- 下一篇: JUC并发编程九 并发架构--循环打印