Skip to content

Instantly share code, notes, and snippets.

@rozaydin
Created March 14, 2017 13:50
Show Gist options
  • Save rozaydin/b76b973cd64719410686d5ca64a7853a to your computer and use it in GitHub Desktop.
Save rozaydin/b76b973cd64719410686d5ca64a7853a to your computer and use it in GitHub Desktop.
Future<T> get() - Blocks till the task completes - No Exception occurs
package org.tutorial.future;
import org.tutorial.future.task.FibonacciTask;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Created by rozaydin on 3/14/17.
*/
public class GetScenario {
private final ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
/**
* Executes the task and call get()
* blocks till the execution is completed
* We expect no execeptions and the calculated result
* to be printed
*/
public void execute() {
Future<Long> result = singleThreadExecutor.submit(() -> FibonacciTask.calculate(30)); // 30 - 832040
singleThreadExecutor.shutdown();
// 1. get result
try {
//
System.out.println("1. calling Future.get() -- this method will block");
long fiboNumber = result.get();
System.out.println("Fibonacci Number is: " + fiboNumber );
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
System.out.println("Task interrupted! " + ie);
} catch (ExecutionException ee) {
System.out.println("Execution exception occured! " + ee);
}
}
public static void main(String[] args) {
GetScenario scenario = new GetScenario();
scenario.execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment