站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JBoss 4.0.1 sp1 Cache API Documentation 英文版文档

IdentityLock (JBossCache API) - JBoss 4.0.1 sp1 Cache API Documentation 英文版文档


org.jboss.cache.lock
Class IdentityLock

java.lang.Object
  extended byorg.jboss.cache.lock.IdentityLock

public class IdentityLock
extends Object

Lock object which grants and releases locks, and associates locks with owners. The methods to acquire and release a lock require an owner (Object). When a lock is acquired, we store the current owner with the lock. When the same owner (but possibly running in a different thread) wants to acquire the lock, it is immediately granted. When an owner different from the one currently owning the lock wants to release the lock, we do nothing (no-op).

Consider the following example:

 FIFOSemaphore lock=new FIFOSemaphore(1);
 lock.acquire();
 lock.acquire();
 lock.release();
 
This would block on the second acquire() (although we currently already hold the lock) because the lock only has 1 permit. So IdentityLock will allow the following code to work properly:
 IdentityLock lock=new IdentityLock();
 lock.readLock().acquire(this, timeout);
 lock.writeLock().acquire(this, timeout);
 lock.release(this);
 
If the owner is null, then the current thread is taken by default. This allow the following code to work:
 IdentityLock lock=new IdentityLock();
 lock.readLock().attempt(null, timeout);
 lock.writeLock().attempt(null, timeout);
 lock.release(null);
 

Note that the Object given as owner is required to implement Object.equals(java.lang.Object). For a use case see the examples in the testsuite.

Version:
$Revision: 1.13.2.1 $
Author:
Bela Ban Apr 11, 2003, Ben Wang July 2003

Constructor Summary
IdentityLock(TreeCache cache, Fqn fqn)
           
 
Method Summary
 boolean acquireReadLock(Object owner, long timeout)
          Acquire a read lock with a timeout period of timeout milliseconds.
 boolean acquireWriteLock(Object owner, long timeout)
          Acquire a write lock with a timeout of timeout milliseconds.
 Fqn getFqn()
           
 List getReaderOwners()
          Return a copy of the reader lock owner in List.
 Object getWriterOwner()
          Return the writer lock owner object.
 boolean isLocked()
          Check if there is a read or write lock
 boolean isOwner(Object o)
          Am I a lock owner?
 boolean isReadLocked()
          Check if there is a read lock.
 boolean isWriteLocked()
          Check if there is a write lock.
 void release(Object owner)
          Release the lock held by the owner.
 void releaseAll()
          Release all locks associated with this instance.
 void releaseForce()
          Same as releaseAll now.
 String toString()
           
 void toString(StringBuffer sb)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IdentityLock

public IdentityLock(TreeCache cache,
                    Fqn fqn)
Method Detail

getFqn

public Fqn getFqn()

getReaderOwners

public List getReaderOwners()
Return a copy of the reader lock owner in List. Size is zero is not available. Note that this list is synchronized.

Returns:

getWriterOwner

public Object getWriterOwner()
Return the writer lock owner object. Null if not available.

Returns:

acquireWriteLock

public boolean acquireWriteLock(Object owner,
                                long timeout)
                         throws LockingException,
                                TimeoutException
Acquire a write lock with a timeout of timeout milliseconds. Note that if the current owner owns a read lock, it will be upgraded automatically. However, if upgrade fails, i.e., timeout, the read lock will be released automatically.

Parameters:
owner - Can't be null.
timeout -
Returns:
boolean True if lock was acquired and was not held before, false if lock was held
Throws:
LockingException
TimeoutException

acquireReadLock

public boolean acquireReadLock(Object owner,
                               long timeout)
                        throws LockingException,
                               TimeoutException
Acquire a read lock with a timeout period of timeout milliseconds.

Parameters:
owner - Can't be null.
timeout -
Returns:
boolean True if lock was acquired and was not held before, false if lock was held
Throws:
LockingException
TimeoutException

release

public void release(Object owner)
Release the lock held by the owner.

Parameters:
owner - Can't be null.

releaseAll

public void releaseAll()
Release all locks associated with this instance.


releaseForce

public void releaseForce()
Same as releaseAll now.


isReadLocked

public boolean isReadLocked()
Check if there is a read lock.

Returns:

isWriteLocked

public boolean isWriteLocked()
Check if there is a write lock.

Returns:

isLocked

public boolean isLocked()
Check if there is a read or write lock

Returns:

isOwner

public boolean isOwner(Object o)
Am I a lock owner?

Parameters:
o -
Returns:

toString

public String toString()

toString

public void toString(StringBuffer sb)


Copyright © 2002 JBoss Group, LLC. All Rights Reserved.