Java Doc for FileLock.java in  » 6.0-JDK-Core » io-nio » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio.channels 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.nio.channels.FileLock

FileLock
abstract public class FileLock (Code)
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 FileChannel.lock(longlongboolean) lock or FileChannel.tryLock(longlongboolean) tryLock methods of the FileChannel class.

A file-lock object is initially valid. It remains valid until the lock is released by invoking the FileLock.release 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 FileLock.isValid 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 FileLock.isShared 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 FileLock.overlaps 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 java.nio.channels.FileChannel.mapmapped 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.
author:
   Mark Reinhold
author:
   JSR-51 Expert Group
version:
   1.15, 07/05/05
since:
   1.4




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

Method Summary
final public  FileChannelchannel()
     Returns the file channel upon whose file this lock is held.
final public  booleanisShared()
     Tells whether this lock is shared.
abstract public  booleanisValid()
     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.

final public  booleanoverlaps(long position, long size)
     Tells whether or not this lock overlaps the given lock range.
final public  longposition()
     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.

abstract public  voidrelease()
     Releases this lock.

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

final public  longsize()
     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.

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


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


Parameters:
  channel - The file channel upon whose file this lock is held
Parameters:
  position - The position within the file at which the locked region starts;must be non-negative
Parameters:
  size - The size of the locked region; must be non-negative, and the sumposition + size must be non-negative
Parameters:
  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
final public FileChannel channel()(Code)
Returns the file channel upon whose file this lock is held.

The file channel



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

true if lock is shared,false if it is exclusive



isValid
abstract public boolean isValid()(Code)
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.

true if, and only if, this lock is valid



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

true if, and only if, this lock and the given lockrange overlap by at least one byte



position
final public long position()(Code)
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.

The position



release
abstract public void release() throws IOException(Code)
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 lockis no longer open
throws:
  IOException - If an I/O error occurs



size
final public long size()(Code)
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.

The size of the locked region



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



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.