tribble.io
Class LockFile

java.lang.Object
  extended by tribble.io.LockFile
All Implemented Interfaces:
Lock

public class LockFile
extends java.lang.Object
implements Lock

Lock file. Implements an exclusive lock/unlock mechanism using a physical file as the underlying synchronization object.

When the lock is acquired, an attempt is made to create the lock file. Multiple attempts ate made, sleeping for a short interval (250 msec) between attempts, until the file is successfully created or the time-out interval is passed.

When the lock is released, the lock file is deleted.

Since:
2006-07-29
Version:
$Revision: 1.1 $ $Date: 2006/08/19 16:14:49 $
Author:
David R. Tribble (david@tribble.com).
Copyright 2006 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.

Field Summary
(package private) static java.lang.String REV
          Revision information.
 
Constructor Summary
LockFile(java.io.File fname)
          Constructor.
 
Method Summary
 boolean acquire(int timeOut)
          Acquire an exclusive lock with this lock file.
 boolean isLocked()
          Determines if a lock has been acquired for this lock file.
 int lockCount()
          Determines the number of nested locks acquired for this lock file.
 boolean release()
          Release the exclusive lock acquired with this lock file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REV

static final java.lang.String REV
Revision information.

See Also:
Constant Field Values
Constructor Detail

LockFile

public LockFile(java.io.File fname)
         throws java.io.IOException
Constructor.

Parameters:
fname - Name of the lock file.
Throws:
java.io.IOException - Thrown if fname is malformed or does not name an accessible file.
Since:
1.1, 2006-07-29
Method Detail

acquire

public boolean acquire(int timeOut)
                throws java.io.IOException
Acquire an exclusive lock with this lock file. If the lock has already been acquired, an internal counter is incremented, allowing for recursive (nested) locks.

Specified by:
acquire in interface Lock
Parameters:
timeOut - The maximum number of milliseconds to wait while attempting to acquire a lock before giving up. A value of zero specifies an unlimited wait time. Note that the resolution of the system clock may be coarser than one millisecond, so this parameter is only an approximation of the actual time-out interval used.
Returns:
True if an exclusive lock was successfully acquired, otherwise false to indicate that the time-out interval expired before a lock could be obtained.
Throws:
java.io.IOException - Thrown if an I/O error occurs.
Since:
1.1, 2006-07-29

release

public boolean release()
                throws java.io.IOException
Release the exclusive lock acquired with this lock file. If the lock has been acquired more than once, an internal lock count is decremented, allowing for recursive (nested) locking. Only when the count becomes zero is the lock file actually released.

Specified by:
release in interface Lock
Returns:
True if the lock was actually released, i.e., if the lock count becomes zero.
Throws:
java.io.IOException - Thrown if an I/O error occurs.
Since:
1.1, 2006-07-29

isLocked

public boolean isLocked()
Determines if a lock has been acquired for this lock file.

Specified by:
isLocked in interface Lock
Returns:
True if a lock is currently acquired for the lock file, otherwise false. zero.
Since:
1.1, 2006-07-29

lockCount

public int lockCount()
Determines the number of nested locks acquired for this lock file.

Specified by:
lockCount in interface Lock
Returns:
The number of active locks acquired on the lock file, or zero if there are none.
Since:
1.1, 2006-07-29