Java Reference
In-Depth Information
be acquired by this channel. The lock that is acquired is owned by the channel, channel , and is automatic-
ally released when the channel is closed. The FileLock class implements the AutoClosable interface so its
close() method is called at the end of the try block to release the lock. You can also release the lock on a
file by explicitly calling the release() method for the FileLock object.
You can call the isValid() method for a FileLock object to determine whether it is valid. A return value
of true indicates a valid lock; otherwise, false is returned indicating that the lock is not valid. Note that
once created, a FileLock object is immutable. It also has no further effect on file access after it has been
invalidated. If you want to lock the file a second time, you must acquire a new lock.
The previous fragment hangs until a lock on the file is acquired. Having your program hang until a lock
is acquired is not an ideal situation because it is quite possible a file could be locked permanently — at least
until the computer is rebooted. This could be because a programming error in another program has locked
the file, in which case your program hangs indefinitely. The tryLock() method for a channel offers an al-
ternative way of requesting a lock that does not block. It either returns a reference to a valid FileLock object
or returns null if the lock could not be acquired. This gives your program a chance to do something else or
retire gracefully:
try (FileChannel fileChannel =
(FileChannel)(Files.newByteChannel(file, EnumSet.of(READ, WRITE))){
FileLock fileLock = channel.tryLock();
if(fileLock == null) {
System.out.println("The file's locked - again!! Oh, I give up...");
System.exit(1);
}
// Work with the locked file...
} catch (IOException e) {
e.printStackTrace();
}
You will later see a better response to a lock than this in an example, but you should get the idea.
Locking Part of a File
Overloaded versions of the lock() and tryLock() methods enable you to specify just the part of the file
you want to obtain a lock on so you don't lock the whole file and enable you to request a shared lock:
lock(long position, long size, boolean shared) Requests a lock on the region of this
channel's file starting at position and of length size . If the last argument is true , the lock re-
quested is a shared lock. If it is false , the lock requested is an exclusive lock. If the lock cannot be
obtained for any reason, the method blocks until the lock can be obtained or the channel is closed
by another thread.
tryLock(long position, long size, boolean shared) Works in the same way as the previ-
ous method, except that null is returned if the requested lock cannot be acquired. This avoids the
potential for hanging your program indefinitely.
The effect of a shared lock is to prevent an exclusive lock being acquired by another program that over-
laps the region that is locked. However, a shared lock does permit another program to acquire a shared lock
on a region of the file that may overlap the region to which the original shared lock applies. This implies that
more than one program may be accessing the same region of the file, so the effect of a shared lock is simply
to ensure that your code is not prevented from doing whatever it is doing by some other program with a
Search WWH ::




Custom Search