The airG android async library is a group of utilities for easier management of asynchronous tasks. This library provides an Executor Service as an appropriately sized thread pool for background execution of tasks (Threadpool.bg()
). An Executor is provided for executing tasks on the UI thread (Threadpool.fg()
).
To learn more, review the javadoc for android-async.
There are also a few utility methods (in AsyncHelper
) to ensure that code is running on or off the UI thread.
The Threadpool
class creates an appropriately sized thread pool (based on the number of CPU cores available) on which you can schedule background tasks. If you'd like to override the size of this thread pool, call the Threadpool.init(Threadpool.Config config)
method prior to using the executors.
An Executor Service is initialized and available for background task execution. To schedule a task for background execution, use Threadpool.bg()
or any of the various Threadpool.submit()
methods. To interact directly with the ExecutorService
, use Threadpool.background()
to get the instance.
To run a task on the foreground, use the Threadpool.fg()
method. Alternatively, you can use the Threadpool.foreground()
method which returns the main thread Executor.
The AsyncHelper
class includes a few static utility methods to determine whether code is running on the main thread as well as methods that ensure (by throwing exceptions) that certain code is running on or off the UI thread.
Think of Promise
as a Future
or Runnable
that informs you when the execution is complete, cancelled, or failed. There are currently 2 implementations of the Promise
interface
- [SimplePromise] (/javadoc/com/airg/android/async/future/SimplePromise.html) can be passed in to your background runnables, which will set the result (or error). The
SimplePromise
implementation will inform your callbacks of the result, error, or cancellation. - [FuturePromise] (/javadoc/com/airg/android/async/future/FuturePromise.html) can be used exactly as you would use a
Future
. In fact, this class extendsFutureTask
to obtain the result and internally uses aSimplePromise
to report the results. - If neither class meets your exact needs, you can implement your own version of
Promise
.
To use the android-async library in your builds, add the following line to your Gradle build script:
compile 'com.airg.android:async:+@aar'
Or to download the library ](https://bintray.com/airgoss/airGOss/async/_latestVersion)
Please refer to the contribution instructions.