gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:06:21 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com