Search code examples
androidbolts-framework

Is there a max number of parallel Bolts-Android Tasks?


I'm using the BoltsFramework (Parse) in an android Application. Let's say I want to launch in background several Parallel Tasks (so several Threads). Is there a limit to avoid to use too much Threads? Or can we put in a queue if the tasks are too many? I would like to avoid call the tasks in series. The use case should be something like launch several task in parallel (huge number) and when All are completed do something….


Solution

  • Bolts-Android uses a thread pool with a queue behind the scenes, so you don't have to worry about managing it yourself. If you want to see how it's implemented, you can see BoltsExecutors.java and AndroidExecutors.java.

    In a JVM environment, it'll use the default Executors.newCachedThreadPool() which has a limit of Integer.MAX_INT simultaneous parallel threads and a synchronous queue when it runs out of threads.

    In an Android environment, it uses a custom pool size that sizes itself depending on how many cores your CPU has and has a synchronous queue when it runs out of threads as well.

    You can also pass in your own Executor in several methods to manage the thread pool yourself.

    https://github.com/BoltsFramework/Bolts-Android/blob/master/bolts-tasks/src/main/java/bolts/BoltsExecutors.java

    https://github.com/BoltsFramework/Bolts-Android/blob/master/bolts-tasks/src/main/java/bolts/AndroidExecutors.java