简体   繁体   English

将值从异步调用返回到run方法

[英]Return a value from asynchronous call to run method

I have a method that has to return a boolean value. 我有一个必须返回布尔值的方法。 The method has an asynchronous call to run method. 该方法具有对run方法的异步调用。 In the run method, i have to set variable in the enclosing method. 在run方法中,我必须在封闭方法中设置变量。 below is my code. 下面是我的代码。

private boolean isTrue() {
    boolean userAnswer;
    Display.getDefault().asyncExec(new Runnable() {
        public void run() {
            userAnswer = MessageDialog.openQuestion(new Shell(), "some message", "some question?");
        }
    });
    return userAnswer;
}   

This code gives error -- "userAnswer" has to be final, and if i make it final i cant assign a value to it. 这段代码给出了错误 - “userAnswer”必须是最终的,如果我做到最后我不能为它分配一个值。 Please suggest a way to handle this scenario. 请建议一种处理此方案的方法。

You can use the java.util.concurrent.FutureTask<V> if you need to adapt a Callable<V> to a Runnable . 如果需要将Callable<V>调整为Runnable可以使用java.util.concurrent.FutureTask<V>

public class UserQuestion implements Callable<Boolean> {

    private String message;
    private String question;

    public UserQuestion(String message, String question) {
        this.message = message;
        this.question = question;
    }

    public Boolean call() throws Exception {
        boolean userAnswer = MessageDialog.openQuestion(new Shell(),
                message, question);
        return Boolean.valueOf(userAnswer);

    }
}

UserQuestion userQuestion = new UserQuestion("some message", "some question?");
FutureTask<Boolean> futureUserAnswer = new FutureTask<Boolean>(userQuestion);
Display.getDefault().asyncExec(futureUserAnswer);
Boolean userAnswer = futureUserAnswer.get();

There'are a lot of patterns to accomplish this. 有很多模式可以实现这一目标。 Maybe the easiest use some sort of callback function, for example: 也许最容易使用某种回调函数,例如:

interface Callback {
    void onSuccess(boolean value);
}

private boolean isTrue(final Callback callback) {
Display.getDefault().asyncExec(new Runnable() {
    public void run() {
        boolean userAnswer = MessageDialog.openQuestion(new Shell(), "some message", "some question?");
        callback.onSuccess(userAnswer);   
    }
});

} }

And invoke method like this: 并调用这样的方法:

isTrue(new Callback() {
            @Override
            public void onSuccess(boolean value) {
                // do some stuff
            }
        });

You can maybe take a look at the Future Interface: 您可以查看Future Interface:

A Future represents the result of an asynchronous computation. Future表示异步计算的结果。 Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. 提供方法以检查计算是否完成,等待其完成,以及检索计算结果。 The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. 只有在计算完成时才能使用方法get检索结果,必要时阻塞直到准备就绪。 Cancellation is performed by the cancel method. 取消由取消方法执行。 Additional methods are provided to determine if the task completed normally or was cancelled. 提供了其他方法来确定任务是否正常完成或被取消。 Once a computation has completed, the computation cannot be cancelled. 计算完成后,无法取消计算。 If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future and return null as a result of the underlying task. 如果您希望为了取消可用性而使用Future但不提供可用的结果,则可以声明Future类型的类型并返回null作为基础任务的结果。

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

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