0

Note: The app I am working on is for PERSONAL use only. I am trying to collect data for my master thesis.

I am trying to start more than 128 AsyncTasks at once, which fails because of the ThreadPoolExecutor. I've seen some answers why it is not working, but no real answer of how to implement it correctly.

As I have a lot of time for uploading my stuff I was considering to just put my MainActivity to sleep before starting the next upload, which does not seem to work as well.

  for (IrisResult s : results) {
         try {
              Thread.sleep(1000);
              mAzureTableManager.addIrisResult(s);
          } catch (InterruptedException e) {
                    e.printStackTrace();
      }
 }

The method addirisResult() actually starts the AsyncTask by protected Void doInBackground(Void... params) {

I am looking for the SIMPLEST solution, not for the best approach!

4ndro1d
  • 2,926
  • 7
  • 35
  • 65
  • @GabeSechan "AsyncTasks all run on one thread" is not true. Read [order of execution](https://developer.android.com/reference/android/os/AsyncTask.html). – ataulm Dec 31 '16 at 12:05
  • @GabeSechan I posted a link that says you can specify parallel execution **since** Honeycomb. Did I misunderstand the official documentation? – ataulm Jan 01 '17 at 11:08

1 Answers1

2
 yourAsyncTask.executeOnExecutor(yourThreadPoolExecutor, params);

And increase your pool size:

 yourThreadPoolExecutor.setMaximumPoolSize(size);

Sets the maximum allowed number of threads. This overrides any value set in the constructor. If the new value is smaller than the current value, excess existing threads will be terminated when they next become idle.

android.developer.com - ThreadPoolExecutor#setMaximumPoolSize(int)

android.developer.com - AsyncTask#executeOnExecutor(Executor, Params...)


as an example:

yourThreadPoolExecutor = Executors.newCachedThreadPool();
yourThreadPoolExecutor.setMaximumPoolSize(256);
yourAsyncTask.executeOnExecutor(yourThreadPoolExecutor, params);
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • So I need to implement a custom ThreadPoolExecutor? @ataulm I guess it is 128, or CPU_COUNT * 2 + 1; according to this answer: http://stackoverflow.com/a/4072832/1177083 – 4ndro1d Dec 30 '16 at 22:53
  • you can just instantiate one, don't have to implement anything custom. Click through `AsyncTask.execute(Object...)` to see that it uses a default one if you don't specify the one you want to use. – ataulm Dec 30 '16 at 22:57
  • ATM I'm using task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) what is yourThreadPoolExecutor? I can't just simply create a new object of ThreadPoolExecutor – 4ndro1d Dec 30 '16 at 23:12
  • you can with something like `Executors.newCachedThreadPool()` https://developer.android.com/reference/java/util/concurrent/Executors.html#newCachedThreadPool() you could also use the `ThreadPoolExecutor` constructor if you wanted to configure all paramaters – Blundell Dec 30 '16 at 23:35
  • Could you please provide a full sample of how to use it? – 4ndro1d Dec 31 '16 at 00:18
  • 1
    edited answer with a simple sample, but you should read the docs – Blundell Dec 31 '16 at 00:23
  • 1
    @GabeSechan [See order of execution](https://developer.android.com/reference/android/os/AsyncTask.html) "If you truly want parallel execution, you can invoke `executeOnExecutor(java.util.concurrent.Executor, Object[])` with `THREAD_POOL_EXECUTOR`." – ataulm Dec 31 '16 at 12:06
  • AndroidRuntime_216_crash: crash in the same process: pool-387-thread-1 - Tasks are being rejected after 128 although I set the maximum pool size to 512 – 4ndro1d Dec 31 '16 at 14:06
  • 1
    you need to do some experimentation yourself, the question is answered. Read up on https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#ThreadPoolExecutor(int,%20int,%20long,%20java.util.concurrent.TimeUnit,%20java.util.concurrent.BlockingQueue) and maybe http://stackoverflow.com/questions/19528304/how-to-get-the-threadpoolexecutor-to-increase-threads-to-max-before-queueing – Blundell Dec 31 '16 at 15:17