Last Updated: February 13, 2015

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

Description:
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.

Example:
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() {
                @Override
                public void run() {
                    if (isFromMainThread()) {
                        Log.d("Fetch", " Runnable Executing From Main Thread");
                    } else {
                        Log.d("Fetch", " Runnable Executing From Background Thread");
                    }
                }
            });

thread.start();

0 comments :

Post a Comment

Your comments are valuable for us !!!