5

I have a method that has to return a boolean value. The method has an asynchronous call to run method. In the run method, i have to set variable in the enclosing method. 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. Please suggest a way to handle this scenario.

LeenaLatesh
  • 169
  • 1
  • 3
  • 12
  • 2
    Apart from the syntax error, the problem is that the boolean will be returned *before* the asynchronous call returns. So not sure what you expect from this method... – assylias Aug 05 '13 at 05:45
  • A method from question [Retrieving Result from OkHttp Asynchronous GET](https://stackoverflow.com/questions/42308439/java-retrieving-result-from-okhttp-asynchronous-get) is what you want. – TongChen Jul 22 '19 at 06:42

3 Answers3

7

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
            }
        });
aim
  • 1,461
  • 1
  • 13
  • 26
  • This method onSucess() will be called asynchronously, I need the value in main thread. If I create a global / instance variable and assign boolean value to it. Still main thread wouldn't know when the value is assigned and when should it read the global value. Have you thought your answer through? – Anurag Awasthi Dec 05 '17 at 11:17
7

You can use the java.util.concurrent.FutureTask<V> if you need to adapt a Callable<V> to a Runnable.

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();
René Link
  • 48,224
  • 13
  • 108
  • 140
  • But note that this will block waiting for `futureUserAnswer` to be executed, so you can as well use `syncExec`. @aim's answer is more useful here. – Alexey Romanov Aug 05 '13 at 08:27
  • Yes that is true. Therefore you should either poll the Future via the isDone() method or do it in a separate thread (not the ui thread of course). It might be also recomended to use the overloaded get(long timeout) method. I also agree that a callback might be better. – René Link Aug 05 '13 at 09:47
2

You can maybe take a look at the Future Interface:

A Future represents the result of an asynchronous computation. 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. 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.

npinti
  • 51,780
  • 5
  • 72
  • 96