AsynchronousFileChannel.Lock Method

Definition

Overloads

Lock()

Acquires an exclusive lock on this channel's file.

Lock(Object, ICompletionHandler)

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(Int64, Int64, Boolean, Object, ICompletionHandler)

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/util/concurrent/Future;", "", ApiSince=26)]
public Java.Util.Concurrent.IFuture? Lock ();
[<Android.Runtime.Register("lock", "()Ljava/util/concurrent/Future;", "", ApiSince=26)>]
member this.Lock : unit -> Java.Util.Concurrent.IFuture

Returns

a Future object representing the pending result

Attributes

Remarks

Acquires an exclusive lock on this channel's file.

This method initiates an operation to acquire an exclusive lock on this channel's file. The method returns a Future representing the pending result of the operation. The Future's Future#get() get method returns the FileLock on successful completion.

An invocation of this method behaves in exactly the same way as the invocation

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

Java documentation for java.nio.channels.AsynchronousFileChannel.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(Object, ICompletionHandler)

Acquires an exclusive lock on this channel's file.

[Android.Runtime.Register("lock", "(Ljava/lang/Object;Ljava/nio/channels/CompletionHandler;)V", "", ApiSince=26)]
[Java.Interop.JavaTypeParameters(new System.String[] { "A" })]
public void Lock (Java.Lang.Object? attachment, Java.Nio.Channels.ICompletionHandler? handler);
[<Android.Runtime.Register("lock", "(Ljava/lang/Object;Ljava/nio/channels/CompletionHandler;)V", "", ApiSince=26)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "A" })>]
member this.Lock : Java.Lang.Object * Java.Nio.Channels.ICompletionHandler -> unit

Parameters

attachment
Object

The object to attach to the I/O operation; can be null

handler
ICompletionHandler

The handler for consuming the result

Attributes

Remarks

Acquires an exclusive lock on this channel's file.

This method initiates an operation to acquire a lock on the given region of this channel's file. The handler parameter is a completion handler that is invoked when the lock is acquired (or the operation fails). The result passed to the completion handler is the resulting FileLock.

An invocation of this method of the form ch.lock(att,handler) behaves in exactly the same way as the invocation

ch.{@link #lock(long,long,boolean,Object,CompletionHandler) lock}(0L, Long.MAX_VALUE, false, att, handler)

Java documentation for java.nio.channels.AsynchronousFileChannel.lock(A, java.nio.channels.CompletionHandler<java.nio.channels.FileLock, ? super A>).

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/util/concurrent/Future;", "GetLock_JJZHandler", ApiSince=26)]
public abstract Java.Util.Concurrent.IFuture? Lock (long position, long size, bool shared);
[<Android.Runtime.Register("lock", "(JJZ)Ljava/util/concurrent/Future;", "GetLock_JJZHandler", ApiSince=26)>]
abstract member Lock : int64 * int64 * bool -> Java.Util.Concurrent.IFuture

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 Future object representing the pending result

Attributes

Remarks

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

This method initiates an operation to acquire a lock on the given region of this channel's file. The method behaves in exactly the same manner as the #lock(long, long, boolean, Object, CompletionHandler) method except that instead of specifying a completion handler, this method returns a Future representing the pending result. The Future's Future#get() get method returns the FileLock on successful completion.

Java documentation for java.nio.channels.AsynchronousFileChannel.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

Lock(Int64, Int64, Boolean, Object, ICompletionHandler)

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

[Android.Runtime.Register("lock", "(JJZLjava/lang/Object;Ljava/nio/channels/CompletionHandler;)V", "GetLock_JJZLjava_lang_Object_Ljava_nio_channels_CompletionHandler_Handler", ApiSince=26)]
[Java.Interop.JavaTypeParameters(new System.String[] { "A" })]
public abstract void Lock (long position, long size, bool shared, Java.Lang.Object? attachment, Java.Nio.Channels.ICompletionHandler? handler);
[<Android.Runtime.Register("lock", "(JJZLjava/lang/Object;Ljava/nio/channels/CompletionHandler;)V", "GetLock_JJZLjava_lang_Object_Ljava_nio_channels_CompletionHandler_Handler", ApiSince=26)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "A" })>]
abstract member Lock : int64 * int64 * bool * Java.Lang.Object * Java.Nio.Channels.ICompletionHandler -> unit

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)

attachment
Object

The object to attach to the I/O operation; can be null

handler
ICompletionHandler

The handler for consuming the result

Attributes

Remarks

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

This method initiates an operation to acquire a lock on the given region of this channel's file. The handler parameter is a completion handler that is invoked when the lock is acquired (or the operation fails). The result passed to the completion handler is the resulting FileLock.

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 two-argument #lock(Object,CompletionHandler) method simply locks a region of size Long#MAX_VALUE. If a lock that overlaps the requested region is already held by this Java virtual machine, or this method has been invoked to lock an overlapping region and that operation has not completed, then this method throws OverlappingFileLockException.

Some operating systems do not support a mechanism to acquire a file lock in an asynchronous manner. Consequently an implementation may acquire the file lock in a background thread or from a task executed by a thread in the associated thread pool. If there are many lock operations outstanding then it may consume threads in the Java virtual machine for indefinite periods.

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.AsynchronousFileChannel.lock(long, long, boolean, A, java.nio.channels.CompletionHandler<java.nio.channels.FileLock, ? super A>).

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