API java : FileLock


java.nio.channels
Class FileLock

java.lang.Object
  extended by java.nio.channels.FileLock

public abstract class FileLock
extends Object

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.

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.

Since:
1.4

Constructor Summary
protected FileLock(FileChannel channel, long position, long size, boolean shared)
          Initializes a new instance of this class.
 
Method Summary
 FileChannel channel()
          Returns the file channel upon whose file this lock is held.
 boolean isShared()
          Tells whether this lock is shared.
abstract  boolean isValid()
          Tells whether or not this lock is valid.
 boolean overlaps(long position, long size)
          Tells whether or not this lock overlaps the given lock range.
 long position()
          Returns the position within the file of the first byte of the locked region.
abstract  void release()
          Releases this lock.
 long size()
          Returns the size of the locked region in bytes.
 String toString()
          Returns a string describing the range, type, and validity of this lock.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileLock

protected FileLock(FileChannel channel,
                   long position,
                   long size,
                   boolean shared)
Initializes a new instance of this class.

Parameters:
channel - The file channel upon whose file this lock is held
position - The position within the file at which the locked region starts; 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 if this lock is shared, false if it is exclusive
Throws:
IllegalArgumentException - If the preconditions on the parameters do not hold
Method Detail

channel

public final FileChannel channel()
Returns the file channel upon whose file this lock is held.

Returns:
The file channel

position

public final long position()
Returns the position within the file of the first byte of the locked region.

A locked region need not be contained within, or even overlap, the actual underlying file, so the value returned by this method may exceed the file's current size.

Returns:
The position

size

public final long size()
Returns the size of the locked region in bytes.

A locked region need not be contained within, or even overlap, the actual underlying file, so the value returned by this method may exceed the file's current size.

Returns:
The size of the locked region

isShared

public final boolean isShared()
Tells whether this lock is shared.

Returns:
true if lock is shared, false if it is exclusive

overlaps

public final boolean overlaps(long position,
                              long size)
Tells whether or not this lock overlaps the given lock range.

Returns:
true if, and only if, this lock and the given lock range overlap by at least one byte

isValid

public abstract boolean isValid()
Tells whether or not this lock is valid.

A lock object remains valid until it is released or the associated file channel is closed, whichever comes first.

Returns:
true if, and only if, this lock is valid

release

public abstract void release()
                      throws IOException
Releases this lock.

If this lock object is valid then invoking this method releases the lock and renders the object invalid. If this lock object is invalid then invoking this method has no effect.

Throws:
ClosedChannelException - If the channel that was used to acquire this lock is no longer open
IOException - If an I/O error occurs

toString

public final String toString()
Returns a string describing the range, type, and validity of this lock.

Overrides:
toString in class Object
Returns:
A descriptive string

Ces informations proviennent du site de http://java.sun.com

Remarques

Contenu

Le contenu de cette page provient du site de Sun, et est généré depuis un cache sur l'infobrol après certains traitements automatisés. La présentation peut donc différer du document original, mais le contenu aussi. Vous pouvez utiliser ce bouton pour afficher la page originale du site de Sun :

Quels sont les motivations de cette démarche?

Maintenir les pages en cache sur différents sites peut offrir plus de disponibilité.

Chaque page est indexée dans la base de donnée, ce qui permet de retrouver facilement les informations, au moyen des sommaires, du moteur de recherche interne, etc.

Des facilités sont mises en place pour que les membres de l'infobrol puissent effectuer des traductions en français des différents documents. Ceci devrait permettre aux débutants en programmation Java de consulter les API en français s'ils maîtrisent moins bien la langue de Shakespeare. Dans le cas où une traduction a été soumise, elle est disponible au moyen d'un lien en bas de page. Si la traduction a été validée, la page s'affiche par défaut en français, et un lien en bas de page permet d'atteindre la version en anglais.

Le code sur l'infobrol est automatiquement coloré selon la syntaxe, et les différents mots clés sont transformés en liens pour accéder rapidement aux informations.

Vous avez la possibilité de partager vos expériences en proposant vos propres extraits de code en utilisant le bouton "ajouter un commentaire" en bas de page. Si vous visitez simplement l'infobrol, vous avez déjà accès à cette fonction, mais si vous étes membre du brol, vous pouvez en plus utiliser des boutons supplémentaires de mise en forme, dont la coloration automatique de vos extraits de codes.

Réseaux sociaux

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.

 

Nuage de mots clés

7 mots clés dont 0 définis manuellement (plus d'information...).

Avertissement

Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.

 

Astuce pour imprimer les couleurs des cellules de tableaux : http://www.gaudry.be/ast-rf-450.html
Aucun commentaire pour cette page

© Ce document issu de l′infobrol est enregistré sous le certificat Cyber PrInterDeposit Digital Numbertection. Enregistrement IDDN n° 5329-2177
Document créé le 31/08/06 00:03, dernière modification le Vendredi 17 Juin 2011, 12:12
Source du document imprimé : http://www.gaudry.be/java-api-rf-java/nio/channels/FileLock.html Document affiché 1 fois ce mois de Juin.
St.Gaudry©07.01.02
Outils (masquer)
||
Recherche (afficher)
Recherche :

Utilisateur (masquer)
Apparence (afficher)
Stats (afficher)
15832 documents
452 astuces.
549 niouzes.
3099 definitions.
447 membres.
8115 messages.

Document genere en :
0,60 seconde

Mises à jour :
Mises à jour du site
Citation (masquer)
Réfléchis, décide et agis!

Marc Levy [Extrait de Et si c'était vrai...]
 
l'infobrol
Nous sommes le Vendredi 01 Juin 2012, 21:41, toutes les heures sont au format GMT+1.00 Heure, heure d'été (+1)