Semaphore tryAcquire release 正确的使用方法

    boolean permit = false;
try {
    permit = semaphore.tryAcquire(1, TimeUnit.SECONDS);
    if (permit) {
        System.out.println("Semaphore acquired");
        sleep(5);
    } else {
        System.out.println("Could not acquire semaphore");
    }
} catch (InterruptedException e) {
    throw new IllegalStateException(e);
} finally {
    if (permit) {
        semaphore.release();
    }
}

转载于:https://www.cnblogs.com/bendantuohai/p/6078796.html

你可能感兴趣的:(java)