欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java 异步实现四种方法--CompletableFuture

最编程 2024-07-16 15:40:06
...
/**
 * CompletableFuture
 */
public class CompletableFutureTest {
    public void test() {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        CompletableFuture<String> c = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                //做处理
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "hello";
            }
        }, executorService);

        c.thenAccept(item -> System.out.println(item));
    }
}

推荐阅读