Junior — Middle
What methods can be used to implement parallel task execution in an application?
sobes.tech AI
Answer from AI
In Android, there are several approaches for parallel task execution:
- AsyncTask (deprecated, but still found in legacy code) — for simple background tasks with UI updates.
- Thread and Runnable — creating and starting threads manually.
- HandlerThread and Handlers — for message processing in a separate thread.
- ExecutorService — a thread pool for managing tasks.
- RxJava — a library for reactive programming with convenient thread handling.
- Coroutines (Kotlin) — a modern and convenient way for asynchronous programming.
- WorkManager — for deferred and guaranteed background tasks.
Example using Kotlin Coroutines:
GlobalScope.launch(Dispatchers.IO) {
// Background task
val result = doSomeWork()
withContext(Dispatchers.Main) {
// UI update
updateUI(result)
}
}