我需要检查运行特定代码的线程是否为主线程(UI线程)。如何实现?
我需要检查运行特定代码的线程是否为主线程(UI线程)。如何实现?
Looper.myLooper() == Looper.getMainLooper()
如果这个返回值为true,那么你就在UI线程上了!
您可以使用以下代码来判断当前线程是否为UI/Main线程
if(Looper.myLooper() == Looper.getMainLooper()) {
// Current Thread is Main Thread.
}
或者你也可以使用这个
if(Looper.getMainLooper().getThread() == Thread.currentThread()) {
// Current Thread is Main Thread.
}
Looper.myLooper()
将返回null。因此,两者都是安全的并且具有相同的结果,但第一个稍微慢一些,因为它在地图中搜索以查找looper及其关联的线程并执行其他操作。 - Saeed Masoumi最好的方式是最清晰、最健壮的方式:
Thread.currentThread().equals( Looper.getMainLooper().getThread() )
或者,如果运行时平台是API级别23(棉花糖6.0)或更高:
Looper.getMainLooper().isCurrentThread()
请查看Looper API。请注意,调用Looper.getMainLooper()
需要同步(请参见源代码)。您可能希望通过存储返回值并重复使用来避免开销。
* 鸣谢greg7gkb和2cupsOfTech
Looper.myLooper() == Looper.getMainLooper()
,但我认为这样不够清晰。我要感谢 greg7gkb。 - Michael Allanequals
方法,所以会退回到 ==
,但这可能会在未来发生改变。因此,我已经更正了答案。 - Michael Allan总结这些解决方案,我认为这是最好的一个:
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
}
});
Thread
类并在其上实现 run
或提供一个 Runnable
实例,然后在此 Thread
实例上调用 start()
。在 Kotlin 中,有一种非常简短的方法来实现它,只需使用 thread{ runCodeHere() }
即可。 - android developer您可以检查
if(Looper.myLooper() == Looper.getMainLooper()) {
// You are on mainThread
}else{
// you are on non-ui thread
}
首先检查它是否是主线程
在 Kotlin 中
fun isRunningOnMainThread(): Boolean {
return Thread.currentThread() == Looper.getMainLooper().thread
}
static boolean isRunningOnMainThread() {
return Thread.currentThread().equals(Looper.getMainLooper().getThread());
}
首先声明一下: 我知道这篇文章有“Android”标签,但我的搜索和“Android”无关,这仅仅是我的搜索结果。因此,对于来到这里的非Android的SO Java用户,请别忘了:
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
}
inline fun <T> ensureNotOnMainThread(block: () -> T): T {
check(Thread.currentThread() != Looper.getMainLooper().thread) { "This function cannot be called on main thread" }
return block()
}
它允许将任何方法与其包装在一起,以下是示例:
fun isInDataBase(id: String) = ensureNotOnMainThread { db.contains(id) }
只需记录此行,它应该打印"main"。
Thread.currentThread().name
您可以在Android DDMS Logcat中验证它,其中进程ID将相同,但线程ID将不同。