2

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.

user2030118
  • 155
  • 3
  • 14
  • sorry about that,but in fact i have two questions here,and i wanted to also explain the reason for needing two seperate tasks. is it too terrible? (i mean the text length) :) – user2030118 May 25 '13 at 23:14
  • It's not just that it is long, but that is not very clear. If you want people to help you, you should make some effort to clearly present your problem. – toto2 May 25 '13 at 23:19
  • ok,i will try to edit it so it will be more clear :) – user2030118 May 25 '13 at 23:20
  • should i add some other code,like the two async tasks i am talking about? I just thought that this is unneccesary and will be too messy – user2030118 May 25 '13 at 23:47
  • @user2030118 it would be really helpful if you could post your executor code also. – Raghunandan May 26 '13 at 04:27
  • I dont have an Executor code... that's why i am using the AsyncTask.THREAD_POOL_EXECUTOR inside the executorOnExecutor() method. Am i missing something? should i write some code for the executor or maybe change some parameters in the Asynctask executor? – user2030118 May 26 '13 at 04:52
  • Are you sure they are getting executed serially? maybe they are getting executed in parallel but it just looks like they are getting executed serially? Or maybe one thread is deadlocked or something IDK. If you execute it on the thread pool executor, then they should be executed in parallel. Are you 100% sure that they are being executed serially even though you specified otherwise? Maybe you can add log messages or something to make sure? – Alex Lockwood May 26 '13 at 05:33
  • I am sure they are beeing executed one after the other because i know how much time it takes the cpu to produce a move in my game,and now he only starts to think when the first thread has finished its job. i am 100% suer about that. and listen one more interesting thing...i now tried to do this with android own animation class and i also encountered the same problem. it seems that there is a problem in making animation and another async task together. please help me!! – user2030118 May 26 '13 at 09:54

3 Answers3

1

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

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • So what is the answer to the OP's question? From the code in the post it seems like he understands the executor concept... so what is the answer? (And also, why is #4 bold?) – Alex Lockwood May 26 '13 at 04:05
  • Also, maybe you shouldn't [**copy and paste**](http://stackoverflow.com/a/16752741/844882) code from your other answers... because the HTTP requests you mention in your code don't make much sense in this context. :) – Alex Lockwood May 26 '13 at 04:07
  • he mentions that he needs paralell execution which is not possible with asynctask. So i highlighted parts of the doc to suggest the same. If he wants to use asynctask he needs to follow the thread rules. hence i posted the same. – Raghunandan May 26 '13 at 04:07
  • @AlexLockwood yes i will edit that part of the answer. thanks for suggesting the same. let me know if i need to make any changes in the answer. Also the example seems unnecessary in this context as the OP is already using the asynctask – Raghunandan May 26 '13 at 04:08
  • 1
    It seems like the OP understands how to use an `AsyncTask` and how to implement one... he also seems to understand that the executor is available only on certain platforms. The real question is whether or not he should be using an `AsyncTask` for this purpose to begin with... and I don't think he should. – Alex Lockwood May 26 '13 at 04:11
  • @AlexLockwood agreed. i edited the post and i also feel the OP understands how to implement asynctask. – Raghunandan May 26 '13 at 04:12
  • @AlexLockwood Executor is added in API level 1. Just for clarification in what context are you suggesting "executor is available only on certain platforms"? – Raghunandan May 26 '13 at 04:18
  • thanks for the answers so far guys. I Thought that the executeOnExecutor() method should work with the AsyncTask like it would with a normal java thread. this is from the docs: public static final Executor SERIAL_EXECUTOR Added in API level 11 An Executor that executes tasks one at a time in serial order. This serialization is global to a particular process. public static final Executor THREAD_POOL_EXECUTOR Added in API level 11 An Executor that can be used to execute tasks in parallel – user2030118 May 26 '13 at 04:32
  • @user2030118 i don't understand why you are getting mixed uo with asynctask and executor. All you need is a executor for parallel execution. why do you need asynctask? – Raghunandan May 26 '13 at 04:34
  • 1
    Available in the sense that tasks are not always executed in parallel by default... this is only the case for devices running before Honeycomb and after Donut (before Donut, the executor was single threaded so there wouldn't be any parallelism there). Sorry, I guess "available" wasn't the right word on my part. – Alex Lockwood May 26 '13 at 04:34
  • the reason i am insist on using AsyncTask is because i am already using 2 async tasks in my app, and can't change that..the code is too heavy to be changed, so i don't have a choice. I also tried to use a normal java thread only for this task, but still can't execute it together (parallely) with the other asynctask..so no luck there – user2030118 May 26 '13 at 04:38
  • @AlexLockwood agreed with the above comment – Raghunandan May 26 '13 at 04:38
  • 1
    Check out the `AsyncTask` source code for [Cupcake](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/os/AsyncTask.java/), [Donut](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.6_r2/android/os/AsyncTask.java/), and [Honeycomb](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/os/AsyncTask.java/). Specifically, check out the `CORE_POOL_SIZE` variable and the addition of the `SERIAL_EXECUTOR` in Honeycomb. Sometimes source code is better than the docs. :P – Alex Lockwood May 26 '13 at 04:45
  • so final word is i cannot execute two Async's at once? what is the alternatived for me? i have to make the rotation animation task work together with the EngineThread task. should i use the android Animation class and just put my bitmap inside an imageView to do rthe animation? – user2030118 May 26 '13 at 04:49
  • 1
    yes. Also the field you mentioned THREAD_POOL_EXECUTOR was added in api level 11. credits to @AlexLockwood for correcting me and taking time to putting up those links for clarification. It helped me learn something new. Thanks for your support and your valuable time much appreciated. Seen very few people as supportive as you are. – Raghunandan May 26 '13 at 04:51
  • yes,i understand that. that's why i am doing the check for the android version..but i am working on 4.2 and it still doesn't work. it executes the tasks in serial fashion and not parallel – user2030118 May 26 '13 at 04:54
0

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • In the animation task (thread), in the do in background() i am changing some variable and updating the UI only from the onProgressUpdate() of that task..which is taking place on the main thread. isn't this is the purpose of Async Tasks? like i wrote in my post, how can i do the animation straight on the UI thread if i am using sleep()? if i try to do it in the main thread its freezing my app – user2030118 May 25 '13 at 23:19
0

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.

Siddharth
  • 9,349
  • 16
  • 86
  • 148