package com.example.demo; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @SpringBootApplication public class DemoApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(com.example.demo.DemoApplication.class, args); } @Override public void run(String... args) throws Exception { CompletableFuture<String> completableFuture = new CompletableFuture<String>(); completableFuture.complete("Future's Result"); String result = completableFuture.get(); System.out.println(result); CompletableFuture<Void> future = CompletableFuture.runAsync(new Runnable() { @Override public void run() { // Simulate a long-running Job try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } System.out.println("I'll run in a separate thread than the main thread."); } }); // Block and wait for the future to complete future.get(); } }
Wednesday, April 17, 2019
CompletableFuture
https://www.callicoder.com/java-8-completablefuture-tutorial/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment