com.microsoft.tfs.util.locking
Class AdvisoryFileLock

java.lang.Object
  extended by com.microsoft.tfs.util.locking.AdvisoryFileLock

public final class AdvisoryFileLock
extends java.lang.Object

Provides advisory locking (at minimum, some platforms may escalate to mandatory) of a filesystem resource. The lock can be held by at most one thread in a VM, and by only one VM across the computer. This is an alternative to FileLock, which is held by all threads in a virtual machine.

When the lock is created, it is initially held (locked). Make sure to call release() to release the lock.

Locks may be used one time only. Once a lock is released it may not be re-acquired. Call create(File, boolean) for a new lock.

Since:
TEE-SDK-10.1
Thread-safety:
thread-safe

Method Summary
static AdvisoryFileLock create(java.io.File lockFile, boolean block)
          Creates an AdvisoryFileLock that mediates access to the given file path.
 boolean equals(java.lang.Object obj)
          
protected  void finalize()
          
 int hashCode()
          
 boolean isReleased()
          Tests whether this AdvisoryFileLock has been released (via release().
 void release()
          Releases the lock so it can be acquired by other threads/processes.
 
Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static AdvisoryFileLock create(java.io.File lockFile,
                                      boolean block)
                               throws java.io.IOException,
                                      java.lang.InterruptedException
Creates an AdvisoryFileLock that mediates access to the given file path. Blocks forever (or until thread interrupted) if block is true, returns immediately if block if false.

Parameters:
lockFile - the file to lock (not null). This file does not have to exist, but it will be created (0 bytes) if it does not
block - if true, this method does not return until the lock is obtained (or the controlling thread is interrupted). If false, the method returns immediately; the value is null if the lock was not immediately available or an AdvisoryFileLock if it was.
Returns:
an AdvisoryFileLock, initially owned. Returns null if and only if block was set to false and the lock was not immediately available.
Throws:
java.io.IOException - if an error occurred accessing the given lock file path on disk.
java.lang.InterruptedException - if this thread was interrupted while waiting for its turn to become the only thread in this VM to lock the given file path.

release

public void release()
             throws java.io.IOException
Releases the lock so it can be acquired by other threads/processes. It is safe to call this method multiple times.

Throws:
java.io.IOException - if an error occured releasing the underlying operating system locks.

isReleased

public boolean isReleased()
Tests whether this AdvisoryFileLock has been released (via release().

Returns:
true if the file lock has been released, false if it has not (the file is still locked)

finalize

protected void finalize()
                 throws java.lang.Throwable

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

hashCode

public int hashCode()

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)

Overrides:
equals in class java.lang.Object


© 2015 Microsoft. All rights reserved.