OverlappingFileLockException in Java with Examples
Last Updated :
30 Sep, 2021
An OverlappingFileLockException is thrown when attempting to acquire a lock that overlaps an existing or pending lock held by this process.
This exception is thrown by the lock( ) and tryLock( ) methods of FileChannel if the requested lock region overlaps a file lock that is already held by some thread in this JVM, or if there is already a thread in this JVM waiting to lock an overlapping region of the same file. The FileChannel file locking mechanism is designed to lock files against concurrent access by two separate processes. Two threads within the same JVM should not attempt to acquire a lock on overlapping regions of the same file, and any attempt to do so causes an exception of this type to be thrown.
Package View
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.IllegalStateException
java.nio.channels.OverlappingFileLockException
Remember: It does implement Serializable interface.
Syntax:
public class OverlappingFileLockException
extends IllegalStateException
Note: An unchecked exception is thrown when an attempt is made to acquire a lock on a region of a file that overlaps a region already locked by the same Java virtual machine, or when another thread is already waiting to lock an overlapping region of the same file.
Constructor Summary
- OverlappingFileLockException(): Constructs an instance of this class.
Implementation:
In this example, we shall show you how to create a shared file lock in Java and handle OverlappingFileLockException. Creating shared file locks using Java NIO Channels implies that one should :
- Create a File object to encapsulate an actual file in the file system that you want to lock to
- Create a random access file stream (read-write). To do so you must first create a RandomAccessFile object to encapsulate the file object created above and open it for read-write operations. Then use the getChannel() API method of the RandomAccessFile object to get the file channel to read/write data from/to
- Acquire an exclusive lock on this channel’s file by using the lock(long, long, boolean) API method of the FileChannel class. This method blocks until the region can be locked or this channel is closed or the invoking thread is interrupted. The boolean attribute marks the lock as shared or not. The method returns a handle to a FileLock class for utilizing the lock
- Alternatively, we could use the tryLock(long, long, boolean) API method of the FileChannel class. This method attempts to acquire an exclusive lock on this channel’s file but does not block; an invocation always returns immediately, either having acquired a lock on the requested region or having failed to do so.
Example 1
Java
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
public class GFG {
public static void main(String[] args)
{
try {
File file = new File( "fileToLock.dat" );
FileChannel channel
= new RandomAccessFile(file, "rw" )
.getChannel();
FileLock lock
= channel.lock( 0 , Long.MAX_VALUE, true );
try {
lock = channel.tryLock( 0 , Long.MAX_VALUE,
true );
}
catch (OverlappingFileLockException e) {
System.out.println(
"Overlapping File Lock Error: "
+ e.getMessage());
}
boolean isShared = lock.isShared();
lock.release();
channel.close();
}
catch (IOException e) {
System.out.println( "I/O Error: "
+ e.getMessage());
}
}
}
|
Output:
Example 2:
Java
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) throws Exception
{
RandomAccessFile file
= new RandomAccessFile( "accounts.txt" , "rw" );
FileChannel channel = file.getChannel();
FileLock lock = null ;
try {
lock = channel.tryLock();
}
catch ( final OverlappingFileLockException e) {
file.close();
channel.close();
}
file.writeChars( "writing after lock" );
TimeUnit.HOURS.sleep( 1 );
lock.release();
file.close();
channel.close();
}
}
|
Output:
It will throw the OverlappingFileLockException, if a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region.
Share your thoughts in the comments
Please Login to comment...