Semaphore使用案例

 案例一:使用acquire()

/**
 * Semaphore使用案例
 * 一开始设置了semaphore = 2, 即最大同时存在的线程为2
 * 在本次案例中我们开启了5个线程,执行任务
 * 每次使用需要调用acquire()方法,如果获取成功,semaphore-1,然后往下执行;执行完毕semaphore+1
 * 否则会阻塞,直到有其他线程释放了semaphore
 */
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(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " " + Thread.currentThread().getName() +  " is waiting to access the printer");
                    semaphore.acquire(); // 获取许可
                    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " " + Thread.currentThread().getName() + " is printing");
                    Thread.sleep(2000); // 假设打印需要2秒钟
                    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " " + Thread.currentThread().getName() + " has finished printing");
                    semaphore.release(); // 释放许可
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start(); // 有5个线程想要访问打印机
        }
    }
}

输出结果

11:30:46 Thread-1 is waiting to access the printer
11:30:46 Thread-2 is waiting to access the printer
11:30:46 Thread-4 is waiting to access the printer
11:30:46 Thread-1 is printing
11:30:46 Thread-0 is waiting to access the printer
11:30:46 Thread-3 is waiting to access the printer
11:30:46 Thread-2 is printing
11:30:48 Thread-1 has finished printing
11:30:48 Thread-2 has finished printing
11:30:48 Thread-4 is printing
11:30:48 Thread-0 is printing
11:30:50 Thread-0 has finished printing
11:30:50 Thread-4 has finished printing
11:30:50 Thread-3 is printing
11:30:52 Thread-3 has finished printing

Process finished with exit code 0

案例二:使用tryAcquire()

/**
 * Semaphore使用tryacquire()方法,如果没有semaphore,则会直接返回false,而不会被阻塞,等待其他线程释放资源
 * 
 */
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(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " " + Thread.currentThread().getName() + " is printing");
                        Thread.sleep(2000); // 假设打印需要2秒钟
                        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " " + Thread.currentThread().getName() + " has finished printing");
                        semaphore.release(); // 释放许可
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " " +  Thread.currentThread().getName() + " failed to acquire the semaphore");
                }
            }).start(); // 有5个线程想要访问打印机
        }
    }
}

输出结果

11:32:58 Thread-4 failed to acquire the semaphore
11:32:58 Thread-2 failed to acquire the semaphore
11:32:58 Thread-3 failed to acquire the semaphore
11:32:58 Thread-0 is printing
11:32:58 Thread-1 is printing
11:33:00 Thread-0 has finished printing
11:33:00 Thread-1 has finished printing

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/xuan__xia/article/details/134504013