public class DistributedCounter { private AtomicInteger count = new AtomicInteger(0); private InterProcessSemaphoreV2 semaphore; public DistributedCounter(CuratorFramework client, String path) { semaphore = new InterProcessSemaphoreV2(client, path, count); } public int getCount() throws Exception { return semaphore.availablePermits(); } public void increment() throws Exception { semaphore.acquire(); count.incrementAndGet(); semaphore.returnSemaphore(); } public void decrement() throws Exception { semaphore.acquire(); count.decrementAndGet(); semaphore.returnSemaphore(); } } CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new RetryNTimes(10, 5000)); client.start();


上一篇:
下一篇:
切换中文