Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查当前线程是否不是主线程_Java_Android_Multithreading - Fatal编程技术网

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

Java 如何检查当前线程是否不是主线程,java,android,multithreading,Java,Android,Multithreading,我需要检查运行某段代码的线程是否是主(UI)线程。我怎样才能做到这一点 Looper.myLooper() == Looper.getMainLooper() 如果返回true,那么您就在UI线程上了 您可以使用下面的代码来了解当前线程是否为UI/主线程 if(Looper.myLooper() == Looper.getMainLooper()) { // Current Thread is Main Thread. } 或者你也可以用这个 if(Looper.getMainLoop

我需要检查运行某段代码的线程是否是主(UI)线程。我怎样才能做到这一点

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

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

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

if(Looper.myLooper() == Looper.getMainLooper()) {
   // Current Thread is Main Thread.
}
或者你也可以用这个

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

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

最好的方法是最清晰、最健壮的方法:*

Thread.currentThread().equals( Looper.getMainLooper().getThread() )
或者,如果运行时平台是API级别23(棉花糖6.0)或更高:

Looper.getMainLooper().isCurrentThread()
看。请注意,调用
Looper.getMainLooper()
涉及同步(请参阅)。您可能希望通过存储返回值并重用它来避免开销


*信贷和

您可以尝试Thread.currentThread().isDaemon()

总结解决方案,我认为这是最好的:

boolean isUiThread = VERSION.SDK_INT >= VERSION_CODES.M 
    ? Looper.getMainLooper().isCurrentThread()
    : Thread.currentThread() == Looper.getMainLooper().getThread();
而且,如果您希望在UI线程上运行某些东西,可以使用以下方法:

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

您可以检查

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

Xamarin.Android
端口:(
C#

用法:

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

请允许我在前言中说: 我承认这篇文章有“安卓”标签,然而,我的搜索与“安卓”无关,这是我的最高搜索结果。为此,对于登陆此处的非AndroidJava用户,不要忘记:

public static void main(String[] args{
    Thread.currentThread().setName("SomeNameIChoose");
    /*...the rest of main...*/
}
设置此选项后,在代码的其他地方,您可以轻松检查是否要在主线程上执行以下操作:

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

在想起这个之前,我有点尴尬,但希望它能帮助其他人

一条简单的Toast消息也可以用作快速检查。

用线程示例演示“在API 23或更高版本下”是什么意思的示例?这对我来说没有多大意义。同样的答案也在下面由安基特发布。@Mike谢谢,我修复了API位。AAnkit实际上更喜欢Looper.myLooper()==Looper.getMainLooper(),我认为这不太清楚。我相信greg7gkb。当Android Studio发出警告时,这是否应该与==或equals()进行比较?@2cupsOfTech仔细想想,这是个好建议。目前,这两个测试在运行时是相同的,因为它们不会覆盖
等于
,因此会返回到
=
,但将来可能会发生变化。因此,我纠正了答案。是否应该认为后者是更安全的选项,因为不能保证任意线程与卢珀关联(假设主线程总是与一个活套相关联)?如果代码不与活套关联,则<代码> Looer-MyLoopReor(<)>代码>将返回null。因此,两者都是安全的,结果相同,但第一个在地图中搜索以查找循环器及其关联线程并执行一些其他操作时,速度稍慢。我不确定UI线程是否是守护进程,但我相信你会这样做。但是,你将如何区别我可以(但不应该)创建的守护进程线程。我在我的web应用程序中进行了测试,它表明UI线程是一个守护进程线程。我在eclipse环境中放置了一些调试断点,并对其进行了验证。线程详细信息显示为线程[http-bio-8080-exec-7,5,main]。单击一些UI页面并检查调试点。此外,即使在线程名称中显示“main”,但在线程对象上调用setDaemon(true)将使其成为守护进程。您没有阅读好的部分。。。我并没有(完全)怀疑它是一个守护进程,我只是告诉你,你不能与其他类似的守护进程线程有所不同。换句话说:主线程可能是守护进程线程,但不是所有的守护进程线程都是主线程。(此处要求识别主线程。)
if(Thread.currentThread().getName().equals("SomeNameIChoose"))
{
    //do something on main thread
}