Post has shared content
File Locking in Java
NIO supports file locking to synchronize access to a file. You have the ability to lock a region of a file or the entire file. The file locking mechanism is handled by the operating system and therefore its exact effect is platform-dependent. On some operating systems, a file lock is advisory, whereas on some, it is mandatory. Since it is handled by the operating system, its effect is visible to other programs as well as to Java programs running in other JVMs.

A token representing a lock on a region of a file.
A file-lock object is created each time a lock is acquired on a file via one of the lock or tryLock methods of the FileChannel class, or the lock or tryLock methods of the AsynchronousFileChannel class.

A file-lock object is initially valid. It remains valid until the lock is released by invoking the release method, by closing the channel that was used to acquire it, or by the termination of the Java virtual machine, whichever comes first. The validity of a lock may be tested by invoking its isValid method.

A file lock is either exclusive or shared. A shared lock prevents other concurrently-running programs from acquiring an overlapping exclusive lock, but does allow them to acquire overlapping shared locks. An exclusive lock prevents other programs from acquiring an overlapping lock of either type. Once it is released, a lock has no further effect on the locks that may be acquired by other programs.

Whether a lock is exclusive or shared may be determined by invoking its isShared method. Some platforms do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock.

The locks held on a particular file by a single Java virtual machine do not overlap. The overlaps method may be used to test whether a candidate lock range overlaps an existing lock.

A file-lock object records the file channel upon whose file the lock is held, the type and validity of the lock, and the position and size of the locked region. Only the validity of a lock is subject to change over time; all other aspects of a lock's state are immutable.

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.

File-lock objects are safe for use by multiple concurrent threads.

Platform dependencies

This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.

Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified. The native file-locking facilities of some systems are merely advisory, meaning that programs must cooperatively observe a known locking protocol in order to guarantee data integrity. On other systems native file locks are mandatory, meaning that if one program locks a region of a file then other programs are actually prevented from accessing that region in a way that would violate the lock. On yet other systems, whether native file locks are advisory or mandatory is configurable on a per-file basis. To ensure consistent and correct behavior across platforms, it is strongly recommended that the locks provided by this API be used as if they were advisory locks.

On some systems, acquiring a mandatory lock on a region of a file prevents that region from being mapped into memory, and vice versa. Programs that combine locking and mapping should be prepared for this combination to fail.

On some systems, closing a channel releases all locks held by the Java virtual machine on the underlying file regardless of whether the locks were acquired via that channel or via another channel open on the same file. It is strongly recommended that, within a program, a unique channel be used to acquire all locks on any given file.

Some network filesystems permit file locking to be used with memory-mapped files only when the locked regions are page-aligned and a whole multiple of the underlying hardware's page size. Some network filesystems do not implement file locks on regions that extend past a certain position, often 230 or 231. In general, great care should be taken when locking files that reside on network filesystems.

#programming   #java  

Post has shared content
How to Read and Write from RandomAccessFile in Java
A FileInputStream lets you read data from a file whereas a FileOutputStream lets you write data to a file. A random access file is a combination of both. Using a random access file, you can read from a file as well as write to the file . Reading and writing...

Post has shared content
How to create thread in Java 8
In this article, we'll see a new way to create thread in Java 8 and also revise the basic two ways to create thread with the help of lambda expression. Main focus of the topic is method reference and how to use thread with lambda expression. If you want to ...

Post has shared content
Syntax for Lambda expression in Java
In this article, we try to understand the very basic use of Lambda expression with syntax and many examples. If you want to look what is Lambda and the basic idea behind it, I encourage to read this my article on the basic . I will try to cover all the basi...

Post has shared content
DelayQueue class and Delayed interface example in Java
In this article, we look into one of the implementations in java.util.concurrent support the extended BlockingQueue interface, that defines blocking versions of put and take i.e, DelayQueue.  An unbounded blocking queue of Delayed elements, in which an elem...

Post has attachment
When you are doing the final testing before submitting the code and NullPointerExeception came.

#programming  
Animated Photo

Post has attachment
Guess the output? (Don't compile the program to answer it.)
(a) hello world!
(b) hello world! null
(c) NullPointerException.
(d) ArrayIndexOutOfBoundsException
Photo

Post has attachment
Guess the output? (Don't compile the program to answer it.)
(a) 0 1 2.0 3.0
(b) 0 1 2 3
(c) 0 1.0 2.0 3.0
(d) Compile/Runtime Error

#programming  
Photo

Post has attachment
Guess the output ?
6 votes
-
votes visible to Public
Poll option image
Default Constructor
Named Constructor
Compile/Runtime Error
100%
Default Constructor named Constructor
0%
Default Constructor
0%
Named Constructor
0%
Compile/Runtime Error

Post has attachment
Guess the output ?

(a) woof
(b) woof woof
(c) It varies
(d) Compile Time Error

#programming   #java  
Photo
Wait while more posts are being loaded