本文介绍了从异步调用运行方法返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我有一个必须返回布尔值的方法.该方法有一个异步调用 run 方法.在运行方法中,我必须在封闭方法中设置变量.下面是我的代码.
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;
}
这段代码给出了错误——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.
推荐答案
如果需要适配一个Callable 到一个 java.util.concurrent.FutureTask;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();
这篇关于从异步调用运行方法返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)