So I cooked up my own version:
public class FutureResult<V> {
volatile V result;
volatile Exception problem;
final FutureTask<V> resultSyncer;
public FutureResult() {
Callable<V> resultReturner = new Callable<V>() {
public V call() throws Exception {
if (problem != null) {
throw problem;
} else {
return result;
}
}
};
resultSyncer = new FutureTask<V>(resultReturner);
}
public void set(V result) {
this.result = result;
resultSyncer.run();
}
public void setException(Exception problem) {
this.problem = problem;
resultSyncer.run();
}
public V get() throws InterruptedException, ExecutionException {
return resultSyncer.get();
}
}
The latest code for this class is available at:
http://jiva.googlecode.com/svn/trunk/jiva/src/net/xofar/util/concurrent/FutureResult.java
Functionality of this nature can, of course, also be implemented with the help of plain-old wait/notify or condition variables. But this seemed like a neat way to accomplish what I wanted...
2 comments:
Hey Lalit,
This is Neeraj here. Thanks for the nice class.
Just wondering how we can use this class? I was thinking this class can work as a replacement of FutureTask ? Is my assumption correct?
java.util.concurrent.FutureTask does the same
Post a Comment