Threads, Executors & Thread Pools Interview Questions
KOTLIN › JVM Concurrency
Why shouldn't you create a new Thread for every background task? What do thread pools solve?
What a strong answer covers: Creating threads is expensive (stack memory and OS cost), unlimited threads can exhaust memory and thrash the CPU, and raw threads have no lifecycle management or result handling. Thread pools reuse a fixed set of threads, queue tasks, bound concurrency, and give you Futures and graceful shutdown.
Name the common ExecutorService types and when you'd use each.
What a strong answer covers: newFixedThreadPool(n) for a bounded number of concurrent CPU tasks; newCachedThreadPool for many short-lived tasks (grows unbounded, risky); newSingleThreadExecutor for serial execution in order; newScheduledThreadPool for delayed or periodic work. In modern Android code, coroutines with dispatchers or WorkManager usually replace direct executor use.
What happened to AsyncTask, and what replaced it?
What a strong answer covers: AsyncTask was deprecated in API 30. It leaked Activities through implicit references, didn't handle configuration changes, ran serially by default since Honeycomb, and its results could arrive after the UI was gone. Replacements: Kotlin coroutines with viewModelScope for UI-bound work, WorkManager for deferrable guaranteed work, and java.util.concurrent executors in Java code.
How do ThreadPoolExecutor's core size, max size and queue interact? Why might a pool never grow past its core size?
What a strong answer covers: New tasks first use core threads; once all are busy, tasks go to the queue; only when the queue is full are extra threads created up to max; beyond that the rejection policy runs. With an unbounded LinkedBlockingQueue, the queue never fills, so the pool never grows beyond the core size and max is ignored. That's why newFixedThreadPool sets core equal to max.
How would you migrate a codebase using executors and callbacks to coroutines without a big-bang rewrite?
What a strong answer covers: Wrap existing executors as dispatchers with asCoroutineDispatcher() so threading stays the same; convert callback APIs with suspendCancellableCoroutine and listeners with callbackFlow; add suspend versions alongside old methods and move callers over gradually; start at the leaf data layer and the ViewModel, keep Java interop with future-returning wrappers, and make sure cancellation is wired through to underlying calls. Measure behaviour before and after.