2

I have to edit huge bitmap files, so I want to use all cores of my device to speed up the process. I thought it would be a good solution to:

  • Count the number of available cores
  • Split the image into [numberofcores] parts
  • For each core create a new thread and let this thread handle one part of the image

How do I make sure all threads are done before I continue with other tasks, like saving the picture or return a message that the bitmap has been processed completely?

Lama
  • 2,886
  • 6
  • 43
  • 59
  • This doesn't directly answer your question, but I wouldn't recommend using `AsyncTask`. Depending on the Android version, it may not run those tasks in parallel. I explain that a bit [here](http://stackoverflow.com/questions/12839384/asynctask-doinbackground-not-called-in-android-tablet/12865486#12865486) – Todd Sjolander Nov 02 '12 at 13:27
  • so no asynctask but simple threads? May I call them from the UI-Thread or do I have to call these threads by an AsyncTask? – Lama Nov 02 '12 at 13:55
  • The main issue is that you can't update the UI in any way from any thread but the UI thread. To get the non-UI threads to update the UI, you can pass the threads a `Handler`, then have the thread run `Handler.post()`, and pass it a `Runnable` like this: `myHandler.post(new Runnable() { public void run() { //Do UI code here } });` – Todd Sjolander Nov 02 '12 at 14:26

3 Answers3

1

Join each worker thread after you've started all of them.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
1

I think you should make another AsyncTask that waits for all the other threads to complete, so in the doInBackground() method of your waiting AsyncTask you can do the following :

while( task1.getStatus()!=AsyncTask.Status.FINISHED
         || task1.getStatus()!= AsyncTask.Status.FINISHED){
    Thread.sleep(500);
}

and then in the onPostExecute() method of your "waiting task" you can continue with the other tasks.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
1

Extend a Handler.Callback class with an array that keeps track of threads processing a particular image (might need to extend Thread too, so that you can specify what portion of the Bitmap the particular thread is processing), a the end of each threads processing sendMessage to a Handler object you create with your extended class instance.

Override handleMessage in the extended class, to receive result of particular thread remove thread from processing array store results of particular thread. When processing array size is 0, put results together.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66