private static void executeSemaphore() { System.out.println("### Semaphore Execution Started... ###"); ISemaphore semaphore = context.getBean("semaphore", ISemaphore.class); semaphore.init(5); System.out.println("Semaphore initialized with 5."); try { semaphore.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Semaphore acquired once."); System.out.println("Available semaphore permits: " + semaphore.availablePermits()); semaphore.release(); System.out.println("Semaphore released."); System.out.println("Available semaphore permits: " + semaphore.availablePermits() + "\n"); }
protected SemaphoreMBean(ISemaphore managedObject, ManagementService service) { super(managedObject, service); this.objectName = service.createObjectName("ISemaphore", managedObject.getName()); }
public static void main(String[] args) throws Exception { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); ISemaphore semaphore = hz.getSemaphore("semaphore"); IAtomicLong resource = hz.getAtomicLong("resource"); for (int i = 0; i < 1000; i++) { System.out.println("At iteration: " + i + ", Active Threads: " + resource.get()); semaphore.acquire(); try { resource.incrementAndGet(); Thread.sleep(1000); resource.decrementAndGet(); } finally { semaphore.release(); } } System.out.println("Finished"); } }
@Override protected int availablePermits() { return sem.availablePermits(); } }
@ManagedAnnotation(value = "release", operation = true) @ManagedDescription("Releases the given number of permits, increasing the number of available permits by that amount") public void release(int permits) { managedObject.release(permits); }
@ManagedAnnotation(value = "increase", operation = true) @ManagedDescription("Increases the number of available permits by the indicated increase. Does not block") public void increase(int increases) { managedObject.increasePermits(increases); }
@ManagedAnnotation(value = "reduce", operation = true) @ManagedDescription("Shrinks the number of available permits by the indicated reduction. Does not block") public void reduce(int reduction) { managedObject.reducePermits(reduction); }
@ManagedAnnotation(value = "drain", operation = true) @ManagedDescription("Acquire and return all permits that are immediately available") public int drain() { return managedObject.drainPermits(); }
@ManagedAnnotation("partitionKey") @ManagedDescription("the partitionKey") public String getPartitionKey() { return managedObject.getPartitionKey(); } }
@ManagedAnnotation("available") public int getAvailable() { return managedObject.availablePermits(); }
@ManagedAnnotation(value = "release", operation = true) @ManagedDescription("Releases the given number of permits, increasing the number of available permits by that amount") public void release(int permits) { managedObject.release(permits); }
@ManagedAnnotation(value = "increase", operation = true) @ManagedDescription("Increases the number of available permits by the indicated increase. Does not block") public void increase(int increases) { managedObject.increasePermits(increases); }
@ManagedAnnotation(value = "reduce", operation = true) @ManagedDescription("Shrinks the number of available permits by the indicated reduction. Does not block") public void reduce(int reduction) { managedObject.reducePermits(reduction); }
@ManagedAnnotation(value = "drain", operation = true) @ManagedDescription("Acquire and return all permits that are immediately available") public int drain() { return managedObject.drainPermits(); }
@ManagedAnnotation("partitionKey") @ManagedDescription("the partitionKey") public String getPartitionKey() { return managedObject.getPartitionKey(); } }
@ManagedAnnotation("available") public int getAvailable() { return managedObject.availablePermits(); }
@ManagedAnnotation("name") public String getName() { return managedObject.getName(); }
protected SemaphoreMBean(ISemaphore managedObject, ManagementService service) { super(managedObject, service); this.objectName = service.createObjectName("ISemaphore", managedObject.getName()); }
@ManagedAnnotation("name") public String getName() { return managedObject.getName(); }