csli.util.thread
Class AsynchResult

java.lang.Object
  extended by csli.util.thread.AsynchResult

public class AsynchResult
extends Object

Used for returning asynchronously from a function. TODO: use generics once we switch to Java 1.5 AsynchResult ar = x.asynchFunctionTakingLong(); _ResultType_ r = (_ResultType_)ar.waitForResult();

Author:
Danilo Mirkovic

Constructor Summary
AsynchResult()
           
 
Method Summary
 void addCallback(AsynchResultCallback cb)
          Add a callback for this result.
 boolean hasResult()
          True if the result is available, false otherwise.
 void release()
          If the result hasn't been set yet, sets it to null and releases a client if it's waiting.
 void setResult(Object o)
          Set the result, and unblock the client waiting for it.
 Object waitForResult()
          Blocks until a result is available, and then returns it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsynchResult

public AsynchResult()
Method Detail

waitForResult

public Object waitForResult()
Blocks until a result is available, and then returns it. May block forever if the result never becomes available.


addCallback

public void addCallback(AsynchResultCallback cb)
Add a callback for this result. Callback is different from listener since it is invoked *only once*. The callback will be notified as soon as the result is available, before the eventual blocking call to waitForResult returns. If the result is already available, the callback is invoked right away.


hasResult

public boolean hasResult()
True if the result is available, false otherwise. No blocking.


setResult

public void setResult(Object o)
Set the result, and unblock the client waiting for it.

Throws:
IllegalStateException - if invoked twice on the same AsynchResult

release

public void release()
If the result hasn't been set yet, sets it to null and releases a client if it's waiting. Calling this multiple times is safe -- but only the first time does anything.