1

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.

Thank you in advance.

Nehalkumar
  • 227
  • 2
  • 15

1 Answers1

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.

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.

Kirankumar Zinzuvadia
  • 1,249
  • 10
  • 17
  • i have one another problem is if my 5 async class is running then how to add more two async class for run and how to manage count of loop – Nehalkumar May 02 '15 at 12:56
  • How many AsyncTasks can you run at once? In most versions of Android, the answer is 128. Why will you generally see exactly 5 AsyncTask threads? AsyncTask thread management is rather confusing, especially since it has changed a lot since the first Android release. Check this answer.http://stackoverflow.com/a/24506243/3728591 – Kirankumar Zinzuvadia May 02 '15 at 13:51