user2030118
user2030118

Reputation: 155

Excecuting two Async Tasks parallely at the same time ( Do i really need this?)

I read probably every tutorial and every forum discussion on that subject but still can't make it happen! and it is very frustrating.
it seems that the way to do it is to use executeOnExecutor() method with - AsyncTask.THREAD_POOL_EXECUTOR, and so i did in my code. but still,the second task only beeing executed only after the first one has finished and not in the same time.
My min sdk version is 2.3 and the maximum is 4.2, so i did the check:

if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.HONEYCOMB) {
    engine.setEnginesTurn(true);
    engineThread = new EngineThread(board,engine,activity,boardView);
    rt = new RotateTask(boardView);
    engineThread.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);  
    rt.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

}
else{
    engine.setEnginesTurn(true);
    engineThread = new EngineThread(board,engine,activity,boardView);
    rt = new RotateTask(boardView);
    engineThread.execute();
    rt.execute();
    }

Ignore the boolean variable here..it is not relevant,and also the other code since you wondered why i didn't post it. it is a mess but all working just fine,execpt for the execution of the two tasks. what am i doing wrong?
The reason i want the two tasks running parallely is: the first tasks is a computation task and the other one is a custom Hourglass image rotating animation while the computer is thinking (Its a game app).

EDIT: Ah.. and just wanted to include that i don't do the animation on the main UI thread is because i use sleep() for the animation ,so can't freeze the main thread.

Upvotes: 2

Views: 892

Answers (3)

Siddharth
Siddharth

Reputation: 9574

Just too much text. Please remove, there is a lot that is not needed.

Your design is complex, simplify it.

Why dont you just start 2 Async Tasks. Why have 2 jobs in 1 async task ? In one async task you can do your background thingy, and the other async task in the Pre and Post you can start your animation and stop your animation.

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

So for parallel execution you can't use asynctask. The above is quoted from the doc. So for parallel execution i suggest you look at executor.

http://developer.android.com/reference/java/util/concurrent/Executor.html

Upvotes: 1

Alex Lockwood
Alex Lockwood

Reputation: 83311

It sounds like you are modifying the UI from the background thread in your AsyncTask. This is not thread safe and is probably causing the problem.

Keep your computation task on a separate thread and move your animation back onto the UI thread and (unless I am missing something) that should do the trick. Remember that anything that is drawn to the screen must be published on the main UI thread.

Upvotes: 0

Related Questions