Semaphore使用acquire和tryAcquire方法案例

Semaphore(信号量)可以用来控制同时访问特定资源的线程数量

acquire()方法:

当使用acquire()方法时,如果没有或许到许可证就会被堵塞,直至获得了许可证。

tryAcquire()方法:

当使用tryAcquire()方法时,如果没有获取到许可证,就会返回false,不会被堵塞。

 acquire()方法

public class SemaphoreExample {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2); // 允许同时访问打印机的线程数量为2

        for (int i = 1; i <= 5; i++) {
            new Thread(() -> {
                try {
                    System.out.println("Thread " + Thread.currentThread().getName() + " is waiting to access the printer");
                    semaphore.acquire(); // 获取许可
                    System.out.println("Thread " + Thread.currentThread().getName() + " is printing");
                    Thread.sleep(2000); // 假设打印需要2秒钟
                    System.out.println("Thread " + Thread.currentThread().getName() + " has finished printing");
                    semaphore.release(); // 释放许可
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start(); // 有5个线程想要访问打印机
        }
    }
}

输出结果 

Thread Thread-0 is waiting to access the printer
Thread Thread-3 is waiting to access the printer
Thread Thread-4 is waiting to access the printer
Thread Thread-1 is waiting to access the printer
Thread Thread-2 is waiting to access the printer
Thread Thread-3 is printing
Thread Thread-0 is printing
Thread Thread-3 has finished printing
Thread Thread-0 has finished printing
Thread Thread-4 is printing
Thread Thread-1 is printing
Thread Thread-4 has finished printing
Thread Thread-1 has finished printing
Thread Thread-2 is printing
Thread Thread-2 has finished printing

tryAcquire()方法


public class SemaphoreExampleTryAcquire {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2); // 允许同时访问打印机的线程数量为2
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                if (semaphore.tryAcquire()) {
                    try {
                        System.out.println("Thread " + Thread.currentThread().getName() + " is printing");
                        Thread.sleep(2000); // 假设打印需要2秒钟
                        System.out.println("Thread " + Thread.currentThread().getName() + " has finished printing");
                        semaphore.release(); // 释放许可
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("Thread " + Thread.currentThread().getName() + " failed to acquire the semaphore");
                }
            }).start(); // 有5个线程想要访问打印机
        }
    }
}

输出结果

Thread Thread-0 is printing
Thread Thread-3 failed to acquire the semaphore
Thread Thread-2 failed to acquire the semaphore
Thread Thread-1 is printing
Thread Thread-4 failed to acquire the semaphore
Thread Thread-1 has finished printing
Thread Thread-0 has finished printing

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值