Java Utililty Methods FileLock

List of utility methods to do FileLock

Description

The list of methods to do FileLock are organized into topic(s).

Method

voidpauseForLock(File f, int frequency, int totalTime)
Waits for a file lock for a given time, with a given frequency.
int waited = 0;
final FileOutputStream out = new FileOutputStream(f);
try {
    FileLock lock = out.getChannel().tryLock();
    try {
        while (lock == null) {
            Thread.sleep(frequency);
            waited += frequency;
...
voidreleaseQuietly(FileLock lock)
release Quietly
try {
    lock.release();
} catch (Throwable t) {
voidreleaseQuietly(final FileLock lock)
Releases the given lock quietly - no logging, no exception
if (null != lock) {
    try {
        lock.release();
    } catch (final IOException io) {
voidreleaseSilent(FileLock fileLock)
release Silent
if (fileLock != null) {
    try {
        fileLock.release();
    } catch (IOException ex) {
voidsetNonBlockingFD(Channel c, boolean blocking)
set Non Blocking FD
if (c instanceof SelectableChannel) {
    ((SelectableChannel) c).configureBlocking(blocking);
FileLocktryLock(File file)
try Lock
FileLock toReturn = null;
try {
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    try {
        FileChannel channel = raf.getChannel();
        toReturn = channel.tryLock();
        raf.writeBytes("lock file for: " + ManagementFactory.getRuntimeMXBean().getName());
    } finally {
...
FileLocktrylock(File file, boolean shared)
Use unlock to release lock and avoid channel leaks.
FileChannel chan = new RandomAccessFile(file, "rw").getChannel();
return chan.tryLock(0, Long.MAX_VALUE, shared);
booleantryLock(File location)
try Lock
if (location == null) {
    location = getDataFolder();
if (exclusiveLock != null) {
    return false;
try {
    FileChannel lock = new RandomAccessFile(new File(location, LOCK_FILE_NAME), "rw").getChannel(); 
...
voidunblockSocket(SelectableChannel... channels)
unblock Socket
for (SelectableChannel ch : channels) {
    ch.configureBlocking(false);
voidunlock(File file)
unlock
FileLock lock = locks.get(file);
if (lock != null) {
    try {
        lock.release();
    } catch (IOException e) {
    locks.remove(file);