Skip to content

Instantly share code, notes, and snippets.

@ova2
Last active May 14, 2021 20:37
Show Gist options
  • Save ova2/6459f714ffc2602cfe43872e30f083e9 to your computer and use it in GitHub Desktop.
Save ova2/6459f714ffc2602cfe43872e30f083e9 to your computer and use it in GitHub Desktop.
class LongRunningTaskService {
public <T> CompletableFuture<T> executeCompletableFuture(Supplier<T> task) {
CompletableFuture<T> completableFuture = new CompletableFuture<>();
new Thread(() -> {
try {
System.out.println("Do something before task execution ...");
completableFuture.complete(task.get());
System.out.println("Do something after task execution ...");
} catch (Exception exception) {
completableFuture.completeExceptionally(exception);
Thread.currentThread().interrupt();
}
}).start();
return completableFuture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment