Junior — Middle
Can you explain what a Worker thread is in the context of multithreaded programming?
sobes.tech AI
Answer from AI
In the context of multithreaded programming on Android, a Worker thread is a separate thread that performs long-running or resource-intensive tasks outside the main (UI) thread. This allows the user interface to remain responsive and not be blocked.
For example, loading data from the network, processing files, or performing complex calculations are done in a Worker thread. In Android, classes like AsyncTask (deprecated), HandlerThread, ExecutorService, or modern components from the WorkManager library are often used for this purpose.
An example of using ExecutorService to run a task in a Worker thread:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
// Long operation
performLongTask();
// UI updates should be done on the main thread
runOnUiThread(() -> updateUI());
});