Creating a Thread Pool

Multiple threads have to be created when you need to run multiple tasks simultaneously. Android has built different classes for multi-threading. To run a task on different data, but with only one execution running at a time, an IntentService has to be used.

ThreadPoolExecutor could be used to automatically run tasks when the resources become available, or to allow multiple tasks to run at the same time. This class runs a task by adding it to the queue, when a thread in its pool becomes free. A thread pool can run multiple parallel instances of a task, so you should ensure that your code is thread-safe. Enclose variables that can be accessed by more than one thread in a synchronized block. This approach will prevent ...

Get Expert Android Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.