ExecutorService
public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(3); FutureCompletableFuturefutureResult = executor.submit(new Add(10, 20)); // 1 Integer intermediateResult = futureResult.get(); // 2 Future finalResult = executor.submit(new Multiply(intermediateResult)); // 3 System.out.println(finalResult.get()); executor.shutdown(); }
public static void main(String[] args) throws ExecutionException, InterruptedException { Integer finalResult = CompletableFuture.supplyAsync(() -> add(10, 20)) .thenApplyAsync(result -> multiply(result)) .get(); System.out.println(finalResult); } public static Integer add(int a, int b) { return a + b; } public static Integer multiply(int result) { return result * 15; }
Sem comentários:
Enviar um comentário