How to run the same asynctask more than once?

Just create another instance and execute it.


Just like threads, AsyncTasks can't be reused. You have to create a new instance every time you want to run one.


You can never execute a thread again, not in Java, not in any other language, once the thread is done with the run() method, you cannot restart it, which is why you are getting the IllegalStateException.

You can however still call the methods on that thread but they will run on the thread that is calling them NOT on a different thread. Therefore you will have to create a new one.


You cannot run same instance of an AsyncTask more than once. Let's assume you have an AsyncTask named MyAsyncTaks and you intend to do something like this,

    MyAsyncTask myAsyncTask = new MyAsyncTaks();
    myAsyncTask.execute(); // Works as expected
    .
    .
    .
    .
    myAsyncTask.execute(); // This will throw you exception

The reason for this is, a thread once finishes its 'run' method, cannot be assigned another task. Here, on the first invocation of execute(), your AsyncTask started running and after doing its job, the thread comes out of run. Naturally, next invocation of execute() will throw you exception.

The simplest way to run this more than once is to create a new instance of MyAsyncTaks and call execute on that.

    MyAsyncTask myAsyncTask = new MyAsyncTaks();
    myAsyncTask.execute(); // Works as expected
    .
    .
    .
    MyAsyncTask myAsyncTask2 = new MyAsyncTaks();
    myAsyncTask2.execute(); // Works as expected

Though its not needed to be mentioned here, one must be aware that post Android SDK version Honeycomb, if your run more than one AsyncTask at once, they actually run sequentially. If you want to run them parallally, use executeOnExecutor instead.