android:ExecutorService: - ExecutorService不等待executorService完成

时间:2016-11-02 06:13:02

标签: java android android-layout android-studio java-ee

我有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
    }

1 个答案:

答案 0 :(得分:1)

看起来对构建回调的构成与回调执行的构成存在一些混淆。

以下代码是创建并提交新的Runnable任务,但Runnable仅打印邮件,然后构建BooleanCallBack。我不确定BooleanCallBack究竟是做什么的,但假设它确实是一个回调,回调中的代码将不会马上执行。

为简单起见,让我们想象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");
            }
        });
    }
});

当您调用future.get()时,它会等待run()方法完成执行,这涉及构建BooleanCallBack但不实际运行它。与构造新Runnable对象的方式类似,并不意味着run()方法中的代码已执行。

因此,如果您的代码调用future.get(),您唯一可以确定的是run()方法已经完成(即您已经获得了“runnable is running”消息)。 NOT BooleanCallBack.onSuccess()已执行。

如果您确实想等待“runnable正在运行的回调”位,那么您需要建立对BooleanCallBack的引用,以便检查其状态。