Attempts to acquire a lock on the given region of this channel's file.
This method does not block. An invocation always returns immediately,
either having acquired a lock on the requested region or having failed to
do so. If it fails to acquire a lock because an overlapping lock is held
by another program then it returns null
. If it fails to acquire
a lock for any other reason then an appropriate exception is thrown.
position
| The position at which the locked region is to start; must be non-negative | |
size
| The size of the locked region; must be non-negative, and the sum
position + size must be non-negative | |
shared
| true to request a shared lock,
false to request an exclusive lock |
null
if the lock could not be acquired
because another program holds an overlapping lock
IllegalArgumentException
| If the preconditions on the parameters do not hold | |
ClosedChannelException
| If this channel is closed | |
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 of the same file | |
NonReadableChannelException
| If shared is true but this channel was not opened for reading | |
NonWritableChannelException
| If shared is false but this channel was not opened for writing | |
IOException
| If some other I/O error occurs |
lock(Object,CompletionHandler), lock(long,long,boolean,Object,CompletionHandler), tryLock()
Diagram: AsynchronousChannel