android - AsyncTask 中的 doInBackground() 方法仅有时执行

标签 android android-asynctask

我正在使用 AsyncTask 从列表中呈现图形。有时它工作正常并且图形被渲染。然而,在某些情况下,图形没有被渲染,原因是 doInBackground() 方法没有被触发。这是 AsyncTask 的代码。

private class HistoryPlotAsync extends AsyncTask<Void, Void, Void> {
    boolean isAnalysisMode = false;
    List<Byte> listECG;
    List<Byte> listHS;

    HistoryPlotAsync(List<Byte> listECG, List<Byte> listHS, List<Byte> listMur, boolean isAnalysisMode) {
        this.listECG = listECG;
        this.listHS = listHS;
        this.isAnalysisMode = isAnalysisMode;
        HistoryPlot.this.multiHsRenderer.setPanEnabled(false, false);
        HistoryPlot.this.multiEcgRenderer.setPanEnabled(false, false);
    }

    protected Void doInBackground(Void... params) {
        if (HistoryPlot.this.pcgPlayer != null) {
            HistoryPlot.this.pcgPlayer.start();
        } else {
            try {
                HistoryPlot.this.startSound();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        int i = 0;
        int loopCounter = 0;
        if (this.listHS != null && this.listHS.size() > 0) {
            loopCounter = this.listHS.size();
        }
        double xValue = 0.0d;
        double xValueEcg = 0.0d;
        do {
            xValueEcg += 0.0015625d;
            if (this.listHS != null && i % 2 == 0) {
                xValue += 0.0032012d;
                hsSeries.add(xValue,listHS.get((i/2)));
            }
            try {
                if (this.listECG != null && i < this.listECG.size()) {
                    ecgSeries.add(xValueEcg, listECG.get(i));
                }
            } catch (Exception e) {
                try {
                    Log.d("HistoryPlot -> ", "doInBackground: Exception " + e.getMessage());
                } catch (Exception e2) {
                    Log.e("HistoryPlot -> ", "Error in Analysis mode point conversion");
                }
            }
            if (i > HistoryPlot.this.refRange && i % 94 == 0) {
                HistoryPlot.this.xMin = HistoryPlot.this.xMin + 0.15d;
                HistoryPlot.this.xMax = HistoryPlot.this.xMax + 0.15d;
                HistoryPlot.this.multiHsRenderer.setXAxisMin(HistoryPlot.this.xMin);
                HistoryPlot.this.multiHsRenderer.setXAxisMax(HistoryPlot.this.xMax);
                HistoryPlot.this.multiEcgRenderer.setXAxisMin(HistoryPlot.this.xMin);
                HistoryPlot.this.multiEcgRenderer.setXAxisMax(HistoryPlot.this.xMax);
            }
            if (i % 16 == 0) {
                publishProgress(new Void[0]);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            i++;
            if (i >= loopCounter) {
                break;
            }
        } while (!HistoryPlot.this.taskHistoryPlot.isCancelled());
        return null;
    }

    protected void onProgressUpdate(Void... values) {
        HistoryPlot.this.mHsChart.repaint();
        HistoryPlot.this.mEcgChart.repaint();
    }

    protected void onPostExecute(Void result) {
        HistoryPlot.this.stopSound();
        HistoryPlot.this.enableReplay();
        HistoryPlot.this.multiHsRenderer.setPanEnabled(true, true);
        HistoryPlot.this.multiEcgRenderer.setPanEnabled(true, true);
    }
}

AsyncTask 是通过 Activity 的 onCreate() 方法中的以下代码执行的。

protected void onCreate(Bundle savedInstance) {
...
this.taskHistoryPlot = new HistoryPlotAsync((List) mapData.get("fileEcg"), (List) mapData.get("fileHs"), (List) mapData.get("fileMur"), isAnalysisMode);
this.taskHistoryPlot.execute();
}

我还尝试使用 executeOnExecutor(THREAD_POOL_EXECUTOR) 方法,而不是使用 execute() 方法,结果相同。

最佳答案

我使用以下代码解决了问题。问题是我只是尝试按原样使用 execute() 方法和 executeOnExecutor() 方法。您需要定义一个 Executor 和一个 Blocking Queue 变量并定义池大小才能使其工作。

static int mCorePoolSize = 60;
static int mMaximumPoolSize = 80;
static int mKeepAliveTime = 10;

static BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(mMaximumPoolSize);
static Executor mCustomThreadPoolExecutor = new ThreadPoolExecutor(mCorePoolSize, mMaximumPoolSize, mKeepAliveTime, TimeUnit.SECONDS, workQueue);

this.taskHistoryPlot = new HistoryPlotAsync((List) mapData.get("fileEcg"), (List) mapData.get("fileHs"), (List) mapData.get("fileMur"), isAnalysisMode);
this.taskHistoryPlot.executeOnExecutor(mCustomThreadPoolExecutor);

关于android - AsyncTask 中的 doInBackground() 方法仅有时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092165/

相关文章:

带有复选框的 Android ListView 在滚动时松散选中的项目

Android studio App图标距离太近

Android ListView使用AsyncTask更新Image Thumbnails导致View回收

Android:下载大文件

android - 我正在将我的启动 Activity 从 "MainActivity"更改为 "RealMain"我在 list 中声明了这个但可惜 MainActivity

java - Android Studio 3.2 更新后,我的所有 FAB 图标都很小并且位于右上角

android - Activity 结束后通知消失

android - 一个 Activity 中的多个异步任务类

Android Asynctask vs Runnable vs timertask vs Service

java - 要从多个类调用的公共(public) AsyncTask