Skip to content

Instantly share code, notes, and snippets.

@mpilone
Created April 13, 2016 16:37
Show Gist options
  • Save mpilone/517005f85dc6632de6467f586deb1897 to your computer and use it in GitHub Desktop.
Save mpilone/517005f85dc6632de6467f586deb1897 to your computer and use it in GitHub Desktop.
public boolean tryAcquire(int timeout) {
SemaphorePermit permit = new SemaphorePermit(semaphoreName, groupName,
acquirerName, null);
// Delegate to the gatekeeper which enforces the permit rules based
// on the semaphore definition. The gatekeeper might be a local library
// or a call to a remote service.
permit = gatekeeperGateway.tryAcquire(permit);
Date expirationDate = permit.getExpirationDate();
if (expirationDate == null) {
// We didn't get the permit. Either sleep or expire the attempt.
// ...
}
else {
// We got the permit. Return true.
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment