[英]android:ExecutorService:- ExecutorService does not wait for executorService to complete
I have an executorService
that does not wait for the executorService
part to complete and it directly returns the return value without waiting below is my code: Please see if I am implementing the executorService
properly and help me correct it if required 我有一个
executorService
,它不等待executorService
部分完成,它直接返回返回值,而无需等待,这是我的代码:请查看我是否在正确实现executorService
,并在需要时帮助我更正
public boolean validateForm() {
flag=true;
executorService = Executors.newSingleThreadExecutor();
Future f = executorService.submit(new Runnable() {
public void run() {
Log.e("FLAGssssss", "" + flag);
checkSourceCode(new BooleanCallBack() {
@Override
public void onSuccess(boolean result) {
Log.e("RESULT ISSSSS", "" + result);
validateCode = result;
Log.e("validateSourceCode ISSSSS", "" + validateSourceCode(result));
if (validateSourceCode(result) == false) {
flag = false;
}
Log.e("FLAG ISSSSS", "" + flag);
}
});
}
});
try {
if (f.get() != null) {
flag = true;
}
Log.e("FUTURE IS", "" + f.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
if (!accept_checkbox.isChecked()) {
Toast.makeText(getActivity().getBaseContext(), "Accept The Terms & Conditions To Proceed", Toast.LENGTH_LONG).show();
// accept_checkbox.requestFocus();
flag = false;
}
if (!validateAddress()) {
flag = false;
}
if (!validatelandmark()) {
flag = false;
}
if (!validateDistrict()) {
flag = false;
}
if (!validatePincode()) {
flag = false;
}
if (!validateFullfillment()) {
flag = false;
}
if (flag) {
saveData();
}
executorService.shutdown();
return flag; //flag is returned even before the executorService above is executed first
}
It looks like there is some confusion on what constitutes a callback from being constructed vs. a callback executing. 在构造回调与执行回调之间,似乎存在一些混淆。
The following code is creating and submitting a new Runnable
task, but the Runnable
is only printing a message and then constructing a BooleanCallBack
. 以下代码创建并提交了一个新的
Runnable
任务,但是Runnable
仅打印一条消息,然后构造一个BooleanCallBack
。 I'm not sure exactly what BooleanCallBack
does, but assuming it really is a callback, the code in the callback will not execute right away. 我不确定
BooleanCallBack
到底做什么,但是假设它确实是一个回调,则回调中的代码不会立即执行。
For simplicity, lets think of the Runnable
like this: 为简单起见,让我们想到这样的
Runnable
:
Future future = executorService.submit(new Runnable() {
public void run() {
sysou("runnable is running");
checkSourceCode(new BooleanCallBack() {
@Override
public void onSuccess(boolean result) {
sysou("callback from the runnable is running");
}
});
}
});
When you invoke future.get()
, it waits for the run()
method to complete execution, which involves constructing the BooleanCallBack
but not actually running it. 调用
future.get()
,它将等待run()
方法完成执行,这涉及构造BooleanCallBack
但实际上并未运行它。 Similar to how constructing a new Runnable
object does not mean the code in the run()
method is executed. 类似于构造新的
Runnable
对象并不意味着执行run()
方法中的代码。
So if your code calls future.get()
, the only thing you can be sure of is that the run() method has completed (ie you have gotten the "runnable is running" message). 因此,如果您的代码调用
future.get()
,则唯一可以确定的是run()方法已完成(即,您已收到“ runnable is running”消息)。 NOT that BooleanCallBack.onSuccess()
has executed. 不是说
BooleanCallBack.onSuccess()
已执行。
If you actually want to wait for the "callback from the runnable is running" bit, then you need to establish a reference to the BooleanCallBack
so you can check its status. 如果您实际上要等待“运行中的回调正在运行”位,则需要建立对
BooleanCallBack
的引用,以便可以检查其状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.