简体   繁体   中英

Java asynchronous return of CompletableFuture

If we have AsyncResponse response variable we can write something like:
CompletableFuture#thenAccept(response::resume) .
I don't understand how can we pass boolean resume(Object response) method from AsyncResponse class to thenAccept() which takes Consumer as parameter, because Consumer return void .

The method reference will just ignore the return value. If you expand the method reference into an anonymous inner class, it would look like this:

completableFuture.thenAccept(new Consumer<Object>() {
        @Override
        public void accept(Object object) {
            response.resume(object);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM