How to check if current thread is not main thread

JavaAndroidMultithreading

Java Problem Overview


I need to check if the thread running a certain piece of code is the main (UI) thread or not. How can I achieve this?

Java Solutions


Solution 1 - Java

Looper.myLooper() == Looper.getMainLooper()

if this returns true, then you're on the UI thread!

Solution 2 - Java

you can use below code to know if current thread is UI/Main thread or not

if(Looper.myLooper() == Looper.getMainLooper()) {
   // Current Thread is Main Thread.
}

or you can also use this

if(Looper.getMainLooper().getThread() == Thread.currentThread()) {
   // Current Thread is Main Thread.
}

Here is similar question

Solution 3 - Java

The best way is the clearest, most robust way: *

Thread.currentThread().equals( Looper.getMainLooper().getThread() )

Or, if the runtime platform is API level 23 (Marshmallow 6.0) or higher:

Looper.getMainLooper().isCurrentThread()

See the Looper API. Note that calling Looper.getMainLooper() involves synchronization (see the source). You might want to avoid the overhead by storing the return value and reusing it.

   * credit greg7gkb and 2cupsOfTech

Solution 4 - Java

Summarizing the solutions, I think that's the best one:

boolean isUiThread = VERSION.SDK_INT >= VERSION_CODES.M 
    ? Looper.getMainLooper().isCurrentThread()
    : Thread.currentThread() == Looper.getMainLooper().getThread();

And, if you wish to run something on the UI thread, you can use this:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
       //this runs on the UI thread
    }
});

Solution 5 - Java

You can check

if(Looper.myLooper() == Looper.getMainLooper()) {
   // You are on mainThread 
}else{
// you are on non-ui thread
}

Solution 6 - Java

Allow me to preface this with: I acknowledged this post has the 'Android' tag, however, my search had nothing to do with 'Android' and this was my top result. To that end, for the non-Android SO Java users landing here, don't forget about:

public static void main(String[] args{
    Thread.currentThread().setName("SomeNameIChoose");
    /*...the rest of main...*/
}

After setting this, elsewhere in your code, you can easily check if you're about to execute on the main thread with:

if(Thread.currentThread().getName().equals("SomeNameIChoose"))
{
    //do something on main thread
}

A bit embarrassed I had searched before remembering this, but hopefully it will help someone else!

Solution 7 - Java

First of all check It is main Thread or not

> In Kotlin

fun isRunningOnMainThread(): Boolean {
    return Thread.currentThread() == Looper.getMainLooper().thread
}

> In Java

static boolean isRunningOnMainThread() {
  return Thread.currentThread().equals(Looper.getMainLooper().getThread());
}

Solution 8 - Java

you can verify it in android ddms logcat where process id will be same but thread id will be different.

Solution 9 - Java

Xamarin.Android port: (C#)

public bool IsMainThread => Build.VERSION.SdkInt >= BuildVersionCodes.M
    ? Looper.MainLooper.IsCurrentThread
    : Looper.MyLooper() == Looper.MainLooper;

Usage:

if (IsMainThread) {
    // you are on UI/Main thread
}

Solution 10 - Java

just log this line, it should print "main".

Thread.currentThread().name

Solution 11 - Java

A simple Toast message works also as a quick check.

Solution 12 - Java

You can try Thread.currentThread().isDaemon()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCharlie-BlakeView Question on Stackoverflow
Solution 1 - JavaCarnalView Answer on Stackoverflow
Solution 2 - JavaAAnkitView Answer on Stackoverflow
Solution 3 - JavaMichael AllanView Answer on Stackoverflow
Solution 4 - Javaandroid developerView Answer on Stackoverflow
Solution 5 - JavaLovekush VishwakarmaView Answer on Stackoverflow
Solution 6 - JavaNekoKikoushiView Answer on Stackoverflow
Solution 7 - JavaKumar SantanuView Answer on Stackoverflow
Solution 8 - JavaVishwanath.MView Answer on Stackoverflow
Solution 9 - JavaMehdi DehghaniView Answer on Stackoverflow
Solution 10 - Javashubham chouhanView Answer on Stackoverflow
Solution 11 - JavaStefan IrndorferView Answer on Stackoverflow
Solution 12 - JavaShailendra SinghView Answer on Stackoverflow