Last Updated: February 13, 2015

Android:How to find on which thread code is exceuting?

As we normally say it code is running in “background thread or UI thread”.

Question: How to find on which thread the code is executing?
This article helps to find on which thread the code is executing in Android.

public static boolean isFromMainThread(){
        return Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId();

Example with thread:
As we say that thread exceute in seperate thread here is the proof:

Thread thread = new Thread(new Runnable() {
                public void run() {
                    if (isFromMainThread()) {
                        Log.d("Fetch", " Runnable Executing From Main Thread");
                    } else {
                        Log.d("Fetch", " Runnable Executing From Background Thread");



Post a Comment

Your comments are valuable for us !!!