FileDocCategorySizeDatePackage
Lock.javaAPI DocGlassfish v2 API8193Fri May 04 22:32:20 BST 2007com.sun.enterprise.util.sync

Lock

public class Lock extends Object
A Lock is a concurrency control class to support mutual exclusion. Some of the additional functionality that is provided by this class w.r.t. synchronized are :
  • time out based lock acquiring semantics
  • supports try lock semantics (isLocked())
  • lock ownership can be determined (getOwner())
The usage pattern is the standard acquire/release protocol.

The usage of Lock can be see as under:


public class MyHashTable {
private Lock lock = new Lock();
.....
.....
public Object get(Key k) {
try {
lock.acquire()
....perform search to get the Object ...
} finally {
lock.release()
}
}

public Object set(Key k, Object o) {
try {
lock.acquire()
....perform operation to insert object ...
} finally {
lock.release()
}
}
}

This version of Lock also supports time out semantics when trying to acquire a lock. If the lock is not acquired in the timeout parameter specified, a TimedOutException is thrown. This would be extremely useful in avoiding deadlock situations especially in I/O related multi-threaded access patterns.

This implementation of Lock is reentrant i.e. if the a thread that has a Lock and tries to acquire the same lock, it would be given the lock

author
Dhiru Pandey 8/1/2000

Fields Summary
private Thread
owner
private int
lockCount
Constructors Summary
Methods Summary
public synchronized voidacquireLock()
This method tries to acquire the lock and waits forever till the lock becomes available. If the lock is available, the invoking thread proceeds else it ends up waiting till the lock becomes available. If the invoking thread has already acquired this lock and tries to acquire it again the lock object allows it to acquire it again.

throws
InterruptedException thrown if the calling thread is interrupted during the execution of acquire()

    // number of times the lock is acquired by the owner
	
                                                                          
	      
		while(! tryGetLock())
			wait();
	
public synchronized voidacquireLock(long waitTime)
This method tries to acquire the lock but waits for waitTime in milliseconds for the lock to be acquired. If the lock is available, the invoking thread proceeds else it ends up waiting for the duration of waitTime. After that if the lock is still no available TimedOutException is thrown. If the invoking thread has already acquired this lock and tries to acquire it again the lock object allows it to acquire it again.

param
waitTime time to wait in milliseconds to acquire the lock
throws
TimedOutException thrown if the lock is not acquired within the timeout parameter specified.
throws
InterruptedException thrown if the calling thread is interrupted during the execution of acquire()

		if (tryGetLock() == false && waitTime != 0) {
			if( waitTime == -1)
				while(! tryGetLock())
					wait();
			else {
				boolean isWaiting = true;
				long startTime = System.currentTimeMillis();
				long timeRemaining = waitTime;
				while( isWaiting) {
					wait( timeRemaining);
					if( tryGetLock())
						return;
					timeRemaining = startTime + waitTime - System.currentTimeMillis();
					isWaiting = (timeRemaining > 0);
				}
				throw new TimedoutException();
			}
		}
	
public java.lang.ThreadgetOwner()
This method can be used to get the thread object that is the owner of the lock.

return
the owner of the lock

 return this.owner; 
public synchronized booleanisLocked()
This method can be used to test if a lock has been acquired/in use.

return
true if lock is still available else false

 return (owner != null); 
public synchronized voidreleaseLock()
This method tries to release the lock and notify other waiting threads that the lock is available to be acquired. If the invoking thread has already acquired the lock multiple times then this method will not notify any waiting threads. In this scenario the invoking thread should call release() as many times on the lock object as acquire() was invoked, only after that other threads waiting for this lock will be notified.

		if(!owner.equals( Thread.currentThread()))
			throw new IllegalMonitorStateException();
		if(--lockCount == 0) {
			owner = null;
		  notify();
    }
	
private booleantryGetLock()

		Thread t = Thread.currentThread();
		if( owner == null) {
			owner = t;
			lockCount = 1;
			return true;
		}
		if( owner == t) {
			++lockCount;
			return true;
		}
		return false;