public final class FileLocks extends Object
FileLock
to coordinate file locks
with other processes. This class maintains a single file lock per file, since multiple concurrent FileLock
s on the same
file are not allowed by the JVM. It also uses reference counts to remove locks only when a named lock is no longer being used.Modifier and Type | Class and Description |
---|---|
protected class |
FileLocks.LockHolder |
protected static class |
FileLocks.WrappedLock |
Modifier and Type | Method and Description |
---|---|
static FileLocks |
get()
Obtain the singleton instance for this virtual machine.
|
protected FileLocks.WrappedLock |
lock(File file,
boolean writeLock,
boolean block) |
FileLocks.WrappedLock |
readLock(File file)
Obtain a read lock for the supplied file.
|
int |
size()
Get the number of named locks.
|
FileLocks.WrappedLock |
tryReadLock(File file)
Try to obtain a read lock for the supplied file.
|
FileLocks.WrappedLock |
tryWriteLock(File file)
Try to obtain a write lock for the supplied file.
|
protected void |
unlock(FileLocks.LockHolder holder,
Lock rawLock) |
FileLocks.WrappedLock |
writeLock(File file)
Obtain a write lock for the supplied file.
|
public static FileLocks get()
public FileLocks.WrappedLock writeLock(File file) throws IOException
Lock.lock()
, Lock.lockInterruptibly()
,
Lock.tryLock()
or Lock.tryLock(long, TimeUnit)
), as those methods will immediately return.file
- the file to be locked; may not be nullIOException
- if there is a problem obtaining the file lockpublic FileLocks.WrappedLock readLock(File file) throws IOException
Lock.lock()
, Lock.lockInterruptibly()
,
Lock.tryLock()
or Lock.tryLock(long, TimeUnit)
), as those methods will immediately return.file
- the file to be locked; may not be nullIOException
- if there is a problem obtaining the file lockpublic FileLocks.WrappedLock tryWriteLock(File file) throws IOException
Lock.lock()
,
Lock.lockInterruptibly()
, Lock.tryLock()
or Lock.tryLock(long, TimeUnit)
), as those methods will
immediately return.
However, if this method returns null
, this method could not obtain the lock.
file
- the file to be locked; may not be nullIOException
- if there is a problem obtaining the file lockpublic FileLocks.WrappedLock tryReadLock(File file) throws IOException
Lock.lock()
,
Lock.lockInterruptibly()
, Lock.tryLock()
or Lock.tryLock(long, TimeUnit)
), as those methods will
immediately return.
However, if this method returns null
, this method could not obtain the lock.
file
- the file to be locked; may not be nullIOException
- if there is a problem obtaining the file lockprotected final FileLocks.WrappedLock lock(File file, boolean writeLock, boolean block) throws IOException
IOException
protected void unlock(FileLocks.LockHolder holder, Lock rawLock)
public int size()
Copyright © 2008-2014 JBoss, a division of Red Hat. All Rights Reserved.