Skip to content

Instantly share code, notes, and snippets.

@wfwei
Created March 2, 2013 11:59
Show Gist options
  • Select an option

  • Save wfwei/5070689 to your computer and use it in GitHub Desktop.

Select an option

Save wfwei/5070689 to your computer and use it in GitHub Desktop.
Callable & Future
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableAndFuture {
/**
* @param args
*/
public static void main(String[] args) {
ExecutorService threadPool = Executors.newSingleThreadExecutor();
Future<String> future = threadPool.submit(new Callable<String>(){
@Override
public String call() throws Exception {
Thread.sleep(3000);
return "future";
}
});
try {
System.out.println("waiting...");
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment