/** * Internal helper method for acquiring a permit. This method checks whether currently * a permit can be acquired and - if so - increases the internal counter. The return * value indicates whether a permit could be acquired. This method must be called with * the lock of this object held. * * @return a flag whether a permit could be acquired */ private boolean acquirePermit() { if (getLimit() <= NO_LIMIT || acquireCount < getLimit()) { acquireCount++; return true; } return false; } }
/** * Returns the number of calls to the {@link #acquire()} method that can * still be performed in the current period without blocking. This method * can give an indication whether it is safe to call the {@link #acquire()} * method without risking to be suspended. However, there is no guarantee * that a subsequent call to {@link #acquire()} actually is not-blocking * because in the mean time other threads may have invoked the semaphore. * * @return the current number of available {@link #acquire()} calls in the * current period */ public synchronized int getAvailablePermits() { return getLimit() - getAcquireCount(); }
/** * Tests creating a new instance. */ @Test public void testInit() { final ScheduledExecutorService service = EasyMock .createMock(ScheduledExecutorService.class); EasyMock.replay(service); final TimedSemaphore semaphore = new TimedSemaphore(service, PERIOD, UNIT, LIMIT); EasyMock.verify(service); assertEquals("Wrong service", service, semaphore.getExecutorService()); assertEquals("Wrong period", PERIOD, semaphore.getPeriod()); assertEquals("Wrong unit", UNIT, semaphore.getUnit()); assertEquals("Statistic available", 0, semaphore .getLastAcquiresPerPeriod()); assertEquals("Average available", 0.0, semaphore .getAverageCallsPerPeriod(), .05); assertFalse("Already shutdown", semaphore.isShutdown()); assertEquals("Wrong limit", LIMIT, semaphore.getLimit()); }
/** * Internal helper method for acquiring a permit. This method checks whether currently * a permit can be acquired and - if so - increases the internal counter. The return * value indicates whether a permit could be acquired. This method must be called with * the lock of this object held. * * @return a flag whether a permit could be acquired */ private boolean acquirePermit() { if (getLimit() <= NO_LIMIT || acquireCount < getLimit()) { acquireCount++; return true; } return false; } }
/** * Internal helper method for acquiring a permit. This method checks whether currently * a permit can be acquired and - if so - increases the internal counter. The return * value indicates whether a permit could be acquired. This method must be called with * the lock of this object held. * * @return a flag whether a permit could be acquired */ private boolean acquirePermit() { if (getLimit() <= NO_LIMIT || acquireCount < getLimit()) { acquireCount++; return true; } return false; } }
/** * Internal helper method for acquiring a permit. This method checks whether currently * a permit can be acquired and - if so - increases the internal counter. The return * value indicates whether a permit could be acquired. This method must be called with * the lock of this object held. * * @return a flag whether a permit could be acquired */ private boolean acquirePermit() { if (getLimit() <= NO_LIMIT || acquireCount < getLimit()) { acquireCount++; return true; } return false; } }
/** * Returns the number of calls to the {@link #acquire()} method that can * still be performed in the current period without blocking. This method * can give an indication whether it is safe to call the {@link #acquire()} * method without risking to be suspended. However, there is no guarantee * that a subsequent call to {@link #acquire()} actually is not-blocking * because in the mean time other threads may have invoked the semaphore. * * @return the current number of available {@link #acquire()} calls in the * current period */ public synchronized int getAvailablePermits() { return getLimit() - getAcquireCount(); }
/** * Returns the number of calls to the {@link #acquire()} method that can * still be performed in the current period without blocking. This method * can give an indication whether it is safe to call the {@link #acquire()} * method without risking to be suspended. However, there is no guarantee * that a subsequent call to {@link #acquire()} actually is not-blocking * because in the mean time other threads may have invoked the semaphore. * * @return the current number of available {@link #acquire()} calls in the * current period */ public synchronized int getAvailablePermits() { return getLimit() - getAcquireCount(); }
/** * Returns the number of calls to the {@link #acquire()} method that can * still be performed in the current period without blocking. This method * can give an indication whether it is safe to call the {@link #acquire()} * method without risking to be suspended. However, there is no guarantee * that a subsequent call to {@link #acquire()} actually is not-blocking * because in the mean time other threads may have invoked the semaphore. * * @return the current number of available {@link #acquire()} calls in the * current period */ public synchronized int getAvailablePermits() { return getLimit() - getAcquireCount(); }