Dela via


FileChannel.Lock Method

Definition

Overloads

Lock()

Acquires an exclusive lock on this channel's file.

Lock(Int64, Int64, Boolean)

Acquires a lock on the given region of this channel's file.

Lock()

Acquires an exclusive lock on this channel's file.

[Android.Runtime.Register("lock", "()Ljava/nio/channels/FileLock;", "")]
public Java.Nio.Channels.FileLock? Lock ();
[<Android.Runtime.Register("lock", "()Ljava/nio/channels/FileLock;", "")>]
member this.Lock : unit -> Java.Nio.Channels.FileLock

Returns

A lock object representing the newly-acquired lock

Attributes

Exceptions

the file channel is closed.

this channel was not opened for writing.

either a lock is already held that overlaps this lock request, or another thread is waiting to acquire a lock that will overlap with this request.

the calling thread was interrupted while waiting to acquire the lock.

the channel was closed while the calling thread was waiting to acquire the lock.

if another I/O error occurs while obtaining the requested lock.

Remarks

Acquires an exclusive lock on this channel's file.

An invocation of this method of the form fc.lock() behaves in exactly the same way as the invocation

fc.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false)

Java documentation for java.nio.channels.FileChannel.lock().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Lock(Int64, Int64, Boolean)

Acquires a lock on the given region of this channel's file.

[Android.Runtime.Register("lock", "(JJZ)Ljava/nio/channels/FileLock;", "GetLock_JJZHandler")]
public abstract Java.Nio.Channels.FileLock? Lock (long position, long size, bool shared);
[<Android.Runtime.Register("lock", "(JJZ)Ljava/nio/channels/FileLock;", "GetLock_JJZHandler")>]
abstract member Lock : int64 * int64 * bool -> Java.Nio.Channels.FileLock

Parameters

position
Int64

The position at which the locked region is to start; must be non-negative

size
Int64

The size of the locked region; must be non-negative, and the sum position&nbsp;+&nbsp;size must be non-negative

shared
Boolean

true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)

Returns

A lock object representing the newly-acquired lock

Attributes

Exceptions

if position or size is negative.

if this channel is closed.

if the requested region overlaps an existing lock or pending lock request.

if the channel is not opened in read-mode but shared is true.

if the channel is not opened in write mode but shared is false.

if this channel is closed by another thread while this method is executing.

if the thread is interrupted while in the state of waiting on the desired file lock.

if another I/O error occurs.

Remarks

Acquires a lock on the given region of this channel's file.

An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first.

If this channel is closed by another thread during an invocation of this method then an AsynchronousCloseException will be thrown.

If the invoking thread is interrupted while waiting to acquire the lock then its interrupt status will be set and a FileLockInterruptionException will be thrown. If the invoker's interrupt status is set when this method is invoked then that exception will be thrown immediately; the thread's interrupt status will not be changed.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argument #lock() method simply locks a region of size Long#MAX_VALUE.

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's FileLock#isShared() isShared method.

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

Java documentation for java.nio.channels.FileChannel.lock(long, long, boolean).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to