【发布时间】:2015-05-02 12:35:11
【问题描述】:
我的要求是在循环中同时获取所有相同异步类调用的状态。
for (int i = 0; i < numberOfTasks; i++) {
int taskId = i + 1;
startTask(taskId, taskDuration, useParallelExecution);
}
private void startTask(int taskId, int taskDuration, boolean useParallelExecution) {
TestTask task = new TestTask(taskId, taskDuration);
if (useParallelExecution) {
// this type of executor uses the following params:
//
// private static final int CORE_POOL_SIZE = 5;
// private static final int MAXIMUM_POOL_SIZE = 128;
// private static final int KEEP_ALIVE = 1;
//
// private static final ThreadFactory sThreadFactory = new ThreadFactory() {
// private final AtomicInteger mCount = new AtomicInteger(1);
//
// public Thread newThread(Runnable r) {
// return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
// }
// };
//
// private static final BlockingQueue<Runnable> sPoolWorkQueue =
// new LinkedBlockingQueue<Runnable>(10);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
// this is the same as calling task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
task.execute();
}
}
private class TestTask extends AsyncTask<Void, Void, Void> /* Params, Progress, Result */ {
private final int id;
private final int duration;
TestTask(int id, int duration) {
this.id = id;
this.duration = duration;
}
@Override
protected Void doInBackground(Void... params) {
int taskExecutionNumber = executedTasksCount.incrementAndGet();
log("doInBackground: entered, taskExecutionNumber = " + taskExecutionNumber);
SystemClock.sleep(duration); // emulates some job
log("doInBackground: is about to finish, taskExecutionNumber = " + taskExecutionNumber);
return null;
}
private void log(String msg) {
Log.d("TestTask #" + id, msg);
}
}
在这里,我必须同时获取所有 TestTask 异步类调用的状态。我对此做了很多研发,但没有得到任何解决方案。任何人都知道如何同时获取同一个异步类调用的状态然后帮助我。
提前谢谢你。
【问题讨论】: