0

Im working on android application which needs to execute asynctask more than once and each one should execute after the previous one finished

Here's a simple snippet to describe my problem

String text1="text1";
String text2="text2";
new asynctask().execute(text1);
new asynctask().execute(text2);

but the problem i faced is the two asynctask runs in parallel not when the previous one finished to execute next one.

user1814596
  • 25
  • 1
  • 7

3 Answers3

0

Implement a state machine that will track the state of the instance. In the onPostExecute() function, call a method that checks the state of the instance, and act appropriately (Call the AsyncTask again if needed, etc)

0

Since you're targeting API 10, the serial thread pool isn't available for your AsyncTask, but you can create your own. You can extend this to whatever AsyncTask features you may need...

First, create your thread pool that will execute things serially, a.k.a., a single thread.

public enum SerialTaskExecutor {
    INSTANCE;

    public static SerialThreadPool getInstance() {
        return INSTANCE;
    }

    private final ExecutorService mExecutor;
    private final Handler mMainHandler;

    private SerialThreadPool() {
        mExecutor = Executors.newSingleThreadExecutor();
        mMainHandler = new Handler(Looper.getMainLooper());
    }

    public void executeTask(final SerialTask<P, R> t, final P... params){
        if(t == null){
            return;
        }

        mExecutor.execute(new Runnable(){

            @Override
            public void run(){
                final R result = t.doInBackground(params);

                mMainHandler.post(new Runnable(){

                    @Override
                    public void run(){
                        t.onMainThread(result);
                    }
                });
            }

        });
    }
}

and then define your serial task interface:

public interface SerialTask<P, R> {
    public R doInBackground(P... params);
    public void onMainThread(R result);
}

finally, instead of implementing AsyncTask, implement SerialTask:

public MyTextTask implements SerialTask<String, Void> {

    @Override
    public Void doInBackground(String... params) {
         // do my string processing here
         return null;
    }

    @Override
    public void onMainThread(Void result) 
         // update ui here
    }
}

Now your simple snippet becomes:

String text1="text1";
String text2="text2";
SerialTaskExecutor exe = SerialTaskExecutor.getInstance();
exe.executeTask(new MyTextTask(), text1);
exe.executeTask(new MyTextTask(), text2);
sddamico
  • 2,130
  • 16
  • 13
0

You can also wait until first AsyncTask get finished by using below lines of code:

String firstTask =  new asynctask().execute(text1).get();

then

String secondTask = new asynctask().execute(text2).get();

It will wait until your first will get finish. But your UI will get blocked.

rupesh
  • 2,865
  • 4
  • 24
  • 50
  • thank you ... that's works but is there any way to make UI not getting blocked ?? – user1814596 Mar 20 '14 at 03:47
  • @user3390830 you can get the status of the asynctask and wait the thread til it gets finish the again check the status if its finished then run the another one here is the link to follow: http://stackoverflow.com/questions/21223534/android-multiple-asynctasks-one-after-another – rupesh Mar 20 '14 at 04:20
  • http://stackoverflow.com/questions/10048958/android-calling-asynctask-right-after-an-another-finished – rupesh Mar 20 '14 at 04:21