My requirement is get state of all same async class call same time in loop.
我的要求是在循环中同时获取所有相同异步类调用的状态。
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);
}
}
Here i have to get state for all TestTask async class call simultaneously. I have done lots of R&D on it but not getting any solution. Anybody know how to get state of same async class call simultaneously then help me.
在这里,我必须同时获取所有TestTask异步类调用的状态。我已经做了很多研发,但没有得到任何解决方案。任何人都知道如何同时获得相同异步类调用的状态然后帮助我。
Thank you in advance.
先感谢您。
1 个解决方案
#1
Here is solution. First you have to save TestTask.class
's instance into some list. For that use HashMap<Integer,TestTask>
because it could be better when you want to check asynctask's status by id. Now you can have list of your asyncTasks
so just create for loop
and check status by getting tasks from your position count of loop.
这是解决方案。首先,您必须将TestTask.class的实例保存到某个列表中。为此,使用HashMap
One other hint, If you want to check status when Activity is finished just save this hashMap and retrieve it when you need to check task's status.
另一个提示,如果要在Activity完成时检查状态,只需保存此hashMap并在需要检查任务状态时检索它。
#1
Here is solution. First you have to save TestTask.class
's instance into some list. For that use HashMap<Integer,TestTask>
because it could be better when you want to check asynctask's status by id. Now you can have list of your asyncTasks
so just create for loop
and check status by getting tasks from your position count of loop.
这是解决方案。首先,您必须将TestTask.class的实例保存到某个列表中。为此,使用HashMap
One other hint, If you want to check status when Activity is finished just save this hashMap and retrieve it when you need to check task's status.
另一个提示,如果要在Activity完成时检查状态,只需保存此hashMap并在需要检查任务状态时检索它。