
aQute.libg.filelock.DirectoryLock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of biz.aQute.bndlib Show documentation
Show all versions of biz.aQute.bndlib Show documentation
bndlib: A Swiss Army Knife for OSGi
The newest version!
package aQute.libg.filelock;
import java.io.File;
import java.util.concurrent.TimeUnit;
public class DirectoryLock {
final File lock;
final long timeout;
final public static String LOCKNAME = ".lock";
public DirectoryLock(File directory, long timeout) {
this.lock = new File(directory, LOCKNAME);
this.lock.deleteOnExit();
this.timeout = TimeUnit.MILLISECONDS.toNanos(timeout);
}
public void release() {
lock.delete();
}
public void lock() throws InterruptedException {
if (lock.mkdir())
return;
final long startNanos = System.nanoTime();
while ((System.nanoTime() - startNanos) < timeout) {
if (lock.mkdir())
return;
Thread.sleep(50L);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy