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();

2 comments :

  1. Good morning! Our platform https://expertpaperwriter.com/educibly-com-review/ focus on keeping our prices low, but unlike other academic writing services online, we never sacrifice quality for the sake of price. We thrive on service orientation; our mission is to increase customer loyalty by providing nonparallel services and after sales support.

    ReplyDelete
  2. Nice content thanks for sharing this information with us i hope you keep writing content like this in future. Yellowstone Vest

    ReplyDelete

Your comments are valuable for us !!!