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 obtainedthrowsIOException if lock wait times out or obtain() throws an IOException boolean locked = obtain(); int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL); int sleepCount = 0; while (!locked) { if (++sleepCount == maxSleepCount) { throw new IOException("Lock obtain timed out: " + this.toString()); } try { Thread.sleep(LOCK_POLL_INTERVAL); } catch (InterruptedException e) { throw new IOException(e.toString()); } locked = obtain(); } return locked;
boolean locked = obtain(); int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL); int sleepCount = 0; while (!locked) { if (++sleepCount == maxSleepCount) { throw new IOException("Lock obtain timed out: " + this.toString()); } try { Thread.sleep(LOCK_POLL_INTERVAL); } catch (InterruptedException e) { throw new IOException(e.toString()); } locked = obtain(); } return locked;
Releases exclusive access.