如何检查当前线程是否不是主线程

时间:2022-02-20 21:03:14

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?

我需要检查运行某段代码的线程是否是主(UI)线程。我如何做到这一点?

6 个解决方案

#1


536  

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

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

如果返回true,那么您就在UI线程上!

#2


94  

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

您可以使用下面的代码来了解当前线程是否是UI/主线程

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

这是同样的问题

#3


26  

The best way is the clearest way: *

最好的方法就是最清楚的方法:*

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

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

或者,如果运行时平台是API级别23 (Marshmallow 6.0)或更高:

Looper.getMainLooper().isCurrentThread()

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

看到电影的API。注意,调用Looper.getMainLooper()涉及到同步化(请参阅源代码)。您可能希望通过存储返回值并重用它来避免开销。

   * credit greg7gkb

*信贷greg7gkb

#4


10  

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:

如果你想在UI线程上运行一些东西,你可以用这个:

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

#5


1  

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

您可以在android ddms logcat中验证它,其中进程id是相同的,但是线程id是不同的。

#6


-4  

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

你可以试试Thread.currentThread().isDaemon()

#1


536  

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

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

如果返回true,那么您就在UI线程上!

#2


94  

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

您可以使用下面的代码来了解当前线程是否是UI/主线程

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

这是同样的问题

#3


26  

The best way is the clearest way: *

最好的方法就是最清楚的方法:*

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

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

或者,如果运行时平台是API级别23 (Marshmallow 6.0)或更高:

Looper.getMainLooper().isCurrentThread()

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

看到电影的API。注意,调用Looper.getMainLooper()涉及到同步化(请参阅源代码)。您可能希望通过存储返回值并重用它来避免开销。

   * credit greg7gkb

*信贷greg7gkb

#4


10  

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:

如果你想在UI线程上运行一些东西,你可以用这个:

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

#5


1  

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

您可以在android ddms logcat中验证它,其中进程id是相同的,但是线程id是不同的。

#6


-4  

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

你可以试试Thread.currentThread().isDaemon()