The Artima Developer Community
Sponsored Link

Java Buzz Forum
A FutureResult based on FutureTask

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Lalit Pant

Posts: 51
Nickname: litan
Registered: Jun, 2007

Lalit Pant is a freelance programmer based out of Dallas, Texas
A FutureResult based on FutureTask Posted: Jun 12, 2007 8:05 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Lalit Pant.
Original Post: A FutureResult based on FutureTask
Feed Title: All Things Runnable / Java
Feed URL: http://feeds.feedburner.com/lalitpant/java
Feed Description: Java related posts on Lalit Pant's Blog
Latest Java Buzz Posts
Latest Java Buzz Posts by Lalit Pant
Latest Posts From All Things Runnable / Java

Advertisement
This morning, I was looking within the java.util.concurrent package for something in the spirit of the FutureResult class in Doug Lea's util.concurrent library. Surprisingly enough, I did not find anything (did I miss something?).

So I cooked up my own version:

public class FutureResult<V> {
volatile V result;

Exception problem;

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...

Read: A FutureResult based on FutureTask

Topic: Unit Testing of J2ME Applications or Using Inheritance in Tests Development Previous Topic   Next Topic Topic: It&#039;s Not A Real Programming Language Until...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use