Created
February 13, 2019 13:48
-
-
Save need4spd/8acd46b271c065f6de2f58f68b05baca to your computer and use it in GitHub Desktop.
Callable and Future sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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