0

I'm calling asyncTask from JobIntentService in following method

 public static void search(final String searchTerm, ApiCallback<SearchResult<Item>> apiCallback) {
        new SimpleTask<>(new SimpleTask.TaskAction<SearchResult<Item>>() {
            @Override
            public SearchResult<Item> doTask() {
                return new SearchResult<>(
                        searchTerm,
                        ArrayUtils.INSTANCE.concat(
                                SearchController.getSearch(Controller.DRUG, searchTerm)                    
                );
            }
        }, apiCallback).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

But getting following exception

 Caused by java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@fbb2c9 rejected from java.util.concurrent.ThreadPoolExecutor@c1a8dce[Running, pool size = 9, active threads = 9, queued tasks = 128, completed tasks = 22]
           at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2078)
           at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:843)
           at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1389)
           at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:651)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ragini
  • 765
  • 1
  • 11
  • 29

1 Answers1

2

I'm calling asyncTask from JobIntentService

It is unlikely that this is a good plan.

Partly, that is because your onHandleWork() method is already on a background thread. You should not need a second one.

Partly, that is because the point of AsyncTask is to do work on the main application thread when background work is finished. There is no reason for a JobIntentService to be attempting to do work on the main application thread.

Partly, that is because your process might be stopped as soon as onHandleWork() returns, killing off your AsyncTasks.

But getting following exception

You have started 137 tasks that are not yet completed, and there is a limit on how many pending AsyncTasks you can start.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491