Skip to content

Instantly share code, notes, and snippets.

@aroranubhav
Created November 18, 2025 05:57
Show Gist options
  • Select an option

  • Save aroranubhav/6eeefe8f4c2642712a490ea13ac95d8e to your computer and use it in GitHub Desktop.

Select an option

Save aroranubhav/6eeefe8f4c2642712a490ea13ac95d8e to your computer and use it in GitHub Desktop.
Running Asynchronous Tasks in Android Using Raw Threads
/**
This example demonstrates how to perform background work in Android using a simple Thread and
return the result safely to the main thread using a Handler. This is helpful for understanding
how Android threading works without coroutines or executors.
**/
public class AsyncWithThreads {
public final String TAG = "AsyncWithThreads";
void runAsyncTask() { // assuming this is called from onCreate()
Log.d(TAG, "runAsyncTask: started!");
Log.d(TAG, "runAsyncTask: " + Thread.currentThread().getName());
// Called on main thread
runBackgroundTask(result -> {
Log.d(TAG, "Listener: " + result); // runs on main thread due to Handler
Log.d(TAG, "Listener: " + Thread.currentThread().getName());
});
Log.d(TAG, "runAsyncTask: ended!");
Log.d(TAG, "runAsyncTask: " + Thread.currentThread().getName());
}
void runBackgroundTask(Listener listener) {
Log.d(TAG, "runBackgroundTask: start!");
Log.d(TAG, "runBackgroundTask: " + Thread.currentThread().getName());
new Thread(() -> {
try {
Thread.sleep(5000); // simulate long-running background work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Post callback result on main thread
new Handler(Looper.getMainLooper()).post(() -> {
Log.d(TAG, "runBackgroundTask: inside main looper: "
+ Thread.currentThread().getName());
listener.onComplete("Completed Task!");
});
Log.d(TAG, "runBackgroundTask: outside main looper: "
+ Thread.currentThread().getName());
}).start();
}
}
interface Listener {
void onComplete(String result);
}
┌───────────────────────────────┐
│ Activity.onCreate() │
└───────────────────────────────┘
┌───────────────────────────────┐
│ runAsyncTask() (MAIN) │
└───────────────────────────────┘
│ calls
┌───────────────────────────────┐
│ runBackgroundTask() (MAIN) │
└───────────────────────────────┘
│ spawns new thread
┌───────────────────────────────────────────────────────────────┐
│ Background Thread (Thread-1) │
└───────────────────────────────────────────────────────────────┘
│ sleep 5 seconds (work simulated)
┌────────────────────────────────────────────┐
│ Post result to main thread using Handler │
└────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────┐
│ Main Thread (Looper) receives callback via Handler │
│ listener.onComplete("Completed Task!") │
└──────────────────────────────────────────────────────────────────┘
┌───────────────────────────────┐
│ Listener logs on MAIN thread │
└───────────────────────────────┘
runAsyncTask: started!
runAsyncTask: main
runBackgroundTask: start!
runBackgroundTask: main
runAsyncTask: ended!
runAsyncTask: main
-- 5 seconds later --
runBackgroundTask: outside main looper: Thread-1
runBackgroundTask: inside main looper: main
Listener: Completed Task!
Listener: main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment