Build 1.0_r1(from source)

java.nio.channels
Class FileLock

java.lang.Object
  extended by java.nio.channels.FileLock

public abstract class FileLock
extends Object

A FileLock represents a locked region of a file.

Locks have certain properties that enable collaborating processes to avoid the lost update problem, or reading inconsistent data.

logically, a file lock can be 'exclusive' or 'shared'. Multiple processes can hold shared locks on the same region of a file, but only a single process can hold an exclusive lock on a given region of a file and no other process can simultaneously hold a shared lock overlapping the exclusive lock. An application can determine whether a FileLock is shared or exclusive via the isShared() API.

Locks held by a particular process cannot overlap one another. Applications can determine whether a proposed lock will overlap by using the overlaps(long, long)) API. Locks held in other processes may overlap locks held in this process.

Locks are shared amongst all threads in the acquiring process, and are therefore unsuitable for intra-process synchronization.

Once a lock is acquired it is immutable in all its state except isValid(). The lock will initially be valid, but may be rendered invalid by explicit removal of the lock, using release(), or implicitly by closing the channel or exiting the process (terminating the JVM).

Platform dependencies

Locks are intended to be true platform operating system file locks, and therefore locks held by the JVM process will be visible to other OS processes.

The characteristics of the underlying OS locks will show through in the Java implementation. For example, some platforms' locks are 'mandatory' -- meaning the operating system enforces the locks on processes that attempt to access locked regions of file; whereas other platforms' locks are only 'advisory' -- meaning that processes are required to collaborate on ensuring locks are acquired and there is a potential for processes not to play well. The only safe answer is to assume that the platform is adopting advisory locks an always acquire shared locks when reading a region of file.

On some platforms the presence of a lock will prevent the file being memory mapped. On some platforms closing a channel on a given file handle will release all the locks held on that file -- even if there are other channels open on the same file (their locks will be released). The safe option here is to ensure that you only acquire locks on a single channel for a particular file, and that becomes the synchronization point.

Further care should be exercised when locking files maintained on network file systems since they often have further limitations.


Constructor Summary
protected FileLock(FileChannel channel, long position, long size, boolean shared)
          Constructor for a new file lock instance for a given channel.
 
Method Summary
 FileChannel channel()
          Returns the lock's FileChannel.
 boolean isShared()
          Returns true if the file lock is shared with other processes and false if it is not.
abstract  boolean isValid()
          Returns whether the receiver is a valid file lock or not.
 boolean overlaps(long start, long length)
          Returns true if the receiver's lock region overlapps the region described in the parameter list,and returns false otherwise.
 long position()
          Returns the lock's starting position in the file.
abstract  void release()
          Releases this particular lock on the file.
 long size()
          Returns the length of the file lock in bytes.
 String toString()
          Returns a string that shows the details of the lock suitable for display to an end user.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileLock

protected FileLock(FileChannel channel,
                   long position,
                   long size,
                   boolean shared)
Constructor for a new file lock instance for a given channel. The constructor enforces the starting position, stretch, and shared status of the lock.

Parameters:
channel - underlying file channel that holds the lock.
position - starting point for the lock.
size - length of lock in number of bytes.
shared - shared status of lock (true is shared, false is exclusive).
Method Detail

channel

public final FileChannel channel()
Returns the lock's FileChannel.

Returns:
the channel.

position

public final long position()
Returns the lock's starting position in the file.

Returns:
the lock position.

size

public final long size()
Returns the length of the file lock in bytes.

Returns:
the size of file lock in bytes.

isShared

public final boolean isShared()
Returns true if the file lock is shared with other processes and false if it is not.

Returns:
true if the lock is a shared lock, and false if it is exclusive.

overlaps

public final boolean overlaps(long start,
                              long length)
Returns true if the receiver's lock region overlapps the region described in the parameter list,and returns false otherwise.

Parameters:
start - the starting position for the comparative lock.
length - the length of the comparative lock.
Returns:
true if there is an overlap, and false otherwise.

isValid

public abstract boolean isValid()
Returns whether the receiver is a valid file lock or not. The lock is valid unless the underlying channel has been closed or it has been explicitly released.

Returns:
true if the lock is valid, and false otherwise.

release

public abstract void release()
                      throws IOException
Releases this particular lock on the file. If the lock is invalid then this method has no effect. Once released the lock becomes invalid.

Throws:
ClosedChannelException - if the channel is already closed when an attempt to release the lock is made.
IOException - some other IO exception occurred.

toString

public final String toString()
Returns a string that shows the details of the lock suitable for display to an end user.

Overrides:
toString in class Object
Returns:
the display string.

Build 1.0_r1(from source)

Please submit a feedback, bug or feature