zoukankan      html  css  js  c++  java
  • spring ReentrantLock

    import lombok.extern.slf4j.Slf4j;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    import static cn.itcast.n2.util.Sleeper.sleep;
    
    @Slf4j(topic = "c.Test24")
    public class Test24 {
        static final Object room = new Object();
        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("有烟没?[{}]", 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("外卖送到没?[{}]", hasTakeout);
                    while (!hasTakeout) {
                        log.debug("没外卖,先歇会!");
                        try {
                            waitTakeoutSet.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    log.debug("可以开始干活了");
                } finally {
                    ROOM.unlock();
                }
            }, "小女").start();
    
            sleep(1);
            new Thread(() -> {
                ROOM.lock();
                try {
                    hasTakeout = true;
                    waitTakeoutSet.signal();
                } finally {
                    ROOM.unlock();
                }
            }, "送外卖的").start();
    
            sleep(1);
    
            new Thread(() -> {
                ROOM.lock();
                try {
                    hasCigarette = true;
                    waitCigaretteSet.signal();
                } finally {
                    ROOM.unlock();
                }
            }, "送烟的").start();
        }
    
    }

    需要注意ReentrantLock使用条件变量唤醒线程,使用lock锁住线程,等于将一个房子分成多个房间,每个持有锁的都有这个房子的钥匙,但是只有拥有对应房间钥匙才能进入对应的房间

  • 相关阅读:
    Tableau如何绘制堆叠柱状图
    Tableau如何绘制多边形地图
    Tableau如何绘制凹凸图
    数据库备份策略设计
    MySQL数据库如何实现增量备份
    Tableau如何绘制瀑布图
    创建.net core ef项目
    asp.net core的ef的连接字符串
    sqlserver的varchar nvarchar区别
    jq的ajax请求写法
  • 原文地址:https://www.cnblogs.com/guosai1500581464/p/13411974.html
Copyright © 2011-2022 走看看