java-多线程-Semaphore(信号量)

 
  • Semaphore(信号量) 
  1. [ˈseməfɔ:(r)]:塞么for
  2. 用于控制对某个公共资源的线程访问量
  3. acquire():获取许可,这个一个阻塞方法,假如没有获取到许可,线程会一直阻塞在这里直到获取到许可为止
  4. release():释放占用的资源

 

  • 我们用一个简单的例子看一下
  1. 场景:有5个人要上厕所,但是只有2个坑位
// 我们定义一个上厕所的线程
// 只有2个坑位,但是现在有5个人要上厕所
public class ThreadShit implements Runnable {

    // 2 表示只有两个坑位
    // false 表示不用根据等待顺序获取坑位,谁抢到是谁的
    private static final Semaphore semaphore = new Semaphore(2,false);

    private String name;

    public ThreadShit(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            // 获取许可(查看有没有空的坑位)
            // 这个一个阻塞方法,假如没有获取到许可,线程会一直阻塞在这里直到获取到许可为止
            semaphore.acquire();
            System.out.println(this.name+" 终于挤进了厕所,霸占了坑位,脸上露出了喜悦的笑容!");
            Thread.sleep(3000);
            System.out.println(this.name+" 拉完屎,轻松走出了厕所!");
            // 释放许可(坑位用完了,归还坑位)
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public static void main(String[] args) {
    // 创建一个线程池
    ExecutorService executor = Executors.newFixedThreadPool(5);
    try {
        // 线程
        Runnable r1 = new ThreadShit("小明");
        Runnable r2 = new ThreadShit("小李");
        Runnable r3 = new ThreadShit("小赵");
        Runnable r4 = new ThreadShit("小周");
        Runnable r5 = new ThreadShit("小魏");
        // 执行任务
        executor.execute(r1);
        executor.execute(r2);
        executor.execute(r3);
        executor.execute(r4);
        executor.execute(r5);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        executor.shutdown();
    }
}

输出:
小李 终于挤进了厕所,霸占了坑位,脸上露出了喜悦的笑容!
小赵 终于挤进了厕所,霸占了坑位,脸上露出了喜悦的笑容!
小赵 拉完屎,轻松走出了厕所!
小李 拉完屎,轻松走出了厕所!
小明 终于挤进了厕所,霸占了坑位,脸上露出了喜悦的笑容!
小魏 终于挤进了厕所,霸占了坑位,脸上露出了喜悦的笑容!
小魏 拉完屎,轻松走出了厕所!
小周 终于挤进了厕所,霸占了坑位,脸上露出了喜悦的笑容!
小明 拉完屎,轻松走出了厕所!
小周 拉完屎,轻松走出了厕所!

从上面输出可以看出,当前只有2个坑位,但是有5个人同时要上厕所,那只能等待,当有人出来时才能有人进去

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值