Skip to content

Instantly share code, notes, and snippets.

@dgouyette
Created October 1, 2012 11:46
Show Gist options
  • Save dgouyette/3811144 to your computer and use it in GitHub Desktop.
Save dgouyette/3811144 to your computer and use it in GitHub Desktop.
FutureTest
import com.google.common.util.concurrent.*;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
public class FutureTest {
private static final Logger LOGGER = LoggerFactory.getLogger(FutureTest.class);
//pool de thread de 10
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
public void doCall(final int i) {
ListenableFuture<String> explosion = executorService.submit(new Callable<String>() {
public String call() {
System.out.println("call:"+i);
return "param";
}
});
//pour gerer le retour
Futures.addCallback(explosion, new FutureCallback<String>() {
// we want this handler to run immediately after we push the big red button!
public void onSuccess(String str) {
System.out.println("success : " + str+" i="+i);
}
public void onFailure(Throwable thrown) {
LOGGER.error("failure", thrown);
}
});
}
@Test
public void test() throws ExecutionException, InterruptedException {
long start = System.nanoTime();
for (int i = 0; i < 100; i++) {
doCall(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment