qflib 0.98.1

de.qfs.lib.util
Class ResourceWaiter

java.lang.Object
  |
  +--de.qfs.lib.util.ResourceWaiter

public class ResourceWaiter
extends java.lang.Object

This class is useful in cases where several Threads are waiting for a limited number of resources. Instead of simply calling wait, a ResourceWaiter can be pushed onto a queue. When a resource becomes available, the next waiter is popped from the queue and handed the resource, so no other Thread can grab it first.

The ThreadPool is an example for a class that makes use of a ResourceWaiter.

Version:
$Revision: 1.3 $
Author:
Gregor Schmid

Constructor Summary
ResourceWaiter()
          Create a new ResourceWaiter
ResourceWaiter(java.lang.Object lock)
          Create a new ResourceWaiter
 
Method Summary
 void handResource(java.lang.Object object)
          Pass an Object to the ResourceWaiter.
 java.lang.Object waitFor(long timeout)
          Wait until either someone hands an Object to the waiter or a timeout is reached.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceWaiter

public ResourceWaiter()
Create a new ResourceWaiter

ResourceWaiter

public ResourceWaiter(java.lang.Object lock)
Create a new ResourceWaiter
Parameters:
lock - The object to wait on. When using a ResourceWaiter from a synchronized method, make sure to use the same lock for the ResourceWaiter that the caller owns, otherwise deadlocks will occur.
Method Detail

waitFor

public java.lang.Object waitFor(long timeout)
Wait until either someone hands an Object to the waiter or a timeout is reached.
Parameters:
timeout - The maximum time to wait in milliseconds. Negative means wait indefinitely.
Returns:
The Object passed to gotIt or null, if the timeout was reached.

handResource

public void handResource(java.lang.Object object)
Pass an Object to the ResourceWaiter.
Parameters:
object - The Object that the ResourceWaiter waited for. This will cause the waitFor method of the ResourceWaiter to continue and hand the object back to its caller.

qflib 0.98.1