Skip to content

Instantly share code, notes, and snippets.

@need4spd
Created February 13, 2019 13:48
Show Gist options
  • Save need4spd/8acd46b271c065f6de2f58f68b05baca to your computer and use it in GitHub Desktop.
Save need4spd/8acd46b271c065f6de2f58f68b05baca to your computer and use it in GitHub Desktop.
Callable and Future sample
import java.util.concurrent.*;
public class FutureAndCallableExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("Main : " + Thread.currentThread().getName());
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> callable = () -> {
System.out.println("Entered Callable");
System.out.println("Callable : " + Thread.currentThread().getName());
Thread.sleep(2000);
// if (true) {
// throw new RuntimeException("EEEEEE");
// }
System.out.println("After sleep");
return "returned result";
};
Future<String> future = executorService.submit(callable);
System.out.println("Future get");
System.out.println(future.get());
executorService.shutdown();
System.out.println("Main out");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment