Typical use might look like:
new Lock.With(directory.makeLock("my.lock")) { public Object doBody() { ... code to execute while locked ... } }.run();
Returns true if the resource is currently locked. Note that one must still call {@link #obtain()} before using the resource.
Attempts to obtain exclusive access and immediately return upon success or failure.returntrue iff exclusive access is obtained
Attempts to obtain an exclusive lock within amount of time given. Currently polls once per second until lockWaitTimeout is passed.paramlockWaitTimeout length of time to wait in msreturntrue if lock was obtainedthrowsLockObtainFailedException if lock wait times outthrowsIOException if obtain() throws IOException failureReason = null; boolean locked = obtain(); int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL); int sleepCount = 0; while (!locked) { if (sleepCount++ == maxSleepCount) { String reason = "Lock obtain timed out: " + this.toString(); if (failureReason != null) { reason += ": " + failureReason; } LockObtainFailedException e = new LockObtainFailedException(reason); if (failureReason != null) { e.initCause(failureReason); } throw e; } try { Thread.sleep(LOCK_POLL_INTERVAL); } catch (InterruptedException e) { throw new IOException(e.toString()); } locked = obtain(); } return locked;
failureReason = null; boolean locked = obtain(); int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL); int sleepCount = 0; while (!locked) { if (sleepCount++ == maxSleepCount) { String reason = "Lock obtain timed out: " + this.toString(); if (failureReason != null) { reason += ": " + failureReason; } LockObtainFailedException e = new LockObtainFailedException(reason); if (failureReason != null) { e.initCause(failureReason); } throw e; } try { Thread.sleep(LOCK_POLL_INTERVAL); } catch (InterruptedException e) { throw new IOException(e.toString()); } locked = obtain(); } return locked;
Releases exclusive access.