简体   繁体   English

在原始线程中的CompletableFuture完成时运行

[英]Run whenComplete of CompletableFuture in original thread

How can I run whenComplete of the CompletableFuture in the original thread that CompletableFuture was created in? 我怎样才能运行whenComplete的的CompletableFuture在原来的线程CompletableFuture在创造的呢?

    // main thread
    CompletableFuture
            .supplyAsync(() -> {
                // some logic here
                return null;
            }, testExecutorService);
            .whenComplete(new BiConsumer<Void, Throwable>() {
                @Override
                public void accept(Void aVoid, Throwable throwable) {
                // run this in the "main" thread 
                }
            });

Expanded example for JavaFx: JavaFx的扩展示例:

    button.setOnClick((evt) -> {
        // the handler of the click event is called by the GUI-Thread
        button.setEnabled(false);
        CompletableFuture.supplyAsync(() -> {
            // some logic here (runs outside of GUI-Thread)
            return something;
        }, testExecutorService);
        .whenComplete((Object result, Throwable ex) -> {
            // this part also runs outside the GUI-Thread
            if (exception != null) {
                // something went wrong, handle the exception 
                Platform.runLater(() -> {
                    // ensure we update the GUI only on the GUI-Thread
                    label.setText(ex.getMessage());
                });
            } else {
                // job finished successfull, lets use the result
                Platform.runLater(() -> {
                    label.setText("Done");
                });
            }
            Platform.runLater(() -> {
                button.setEnabled(true); // lets try again if needed
            });
        });
    });

This is not the best code you could write in this situation, but it should bring the point across. 在这种情况下,这不是您可以编写的最佳代码,但是应该可以理解这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止在上下文线程中执行CompletableFuture#whenComplete - How to prevent CompletableFuture#whenComplete execution in context thread completableFuture whenComplete 奇怪的行为 - completableFuture whenComplete strange behavior 来自CompletableFuture的whenComplete的签名 - Signature of whenComplete from CompletableFuture Java CompletableFuture.allOf().whenComplete() 有多个异常 - Java CompletableFuture.allOf().whenComplete() with multiple exceptions 如果使用thenApply,则CompletableFuture#whenComplete未被调用 - CompletableFuture#whenComplete not called if thenApply is used 是否可以通过whenComplete(…)向CompletableFuture添加多个动作? - Is it possible to add multiple actions to a CompletableFuture with whenComplete(…)? 在 CompletableFuture 的 whenComplete() 之前返回的方法 - Method returning before CompletableFuture's whenComplete() 在 CompletableFuture 中返回 autocloseable object 并在 whenComplete 中使用它 - Return autocloseable object inside a CompletableFuture and use it in whenComplete 在与执行线程相同的线程或不同的线程上运行 completablefuture - Run a completablefuture on same thread as executing thread or on a different thread CompletableFuture 是否保证运行新线程? - Is CompletableFuture guaranteed to run un a new thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM