Is there a robust way to detect if Thread.currentThread()
is the Android system UI thread in an application?
I would like to put some asserts in my model code that asserts that only one thread (eg the ui thread) accesses my state, to assure that no kind of synchronization is necessary.
-
See my answer here: http://stackoverflow.com/a/41280460/878126 – android developer Dec 22 '16 at 10:09
7 Answers
Common practice to determine the UI Thread's identity is via Looper#getMainLooper:
if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
// On UI thread.
} else {
// Not on UI thread.
}
From API level 23 and up, there's a slightly more readable approach using new helper method isCurrentThread on the main looper:
if (Looper.getMainLooper().isCurrentThread()) {
// On UI thread.
} else {
// Not on UI thread.
}

- 4,214
- 1
- 22
- 29
-
6http://stackoverflow.com/questions/11411022/how-to-check-if-current-thread-is-not-main-thread This answer is nice too) – UnknownJoe Feb 06 '14 at 20:58
I think that best way is this:
if (Looper.getMainLooper().equals(Looper.myLooper())) {
// UI thread
} else {
// Non UI thread
}

- 15,960
- 6
- 46
- 50
-
3No need to use `equals` since we're only comparing references, and on top of that, they're both static. – mr5 Apr 10 '17 at 07:41
As of API level 23 the Looper
has a nice helper method isCurrentThread
. You could get the mainLooper
and see if it's the one for the current thread this way:
Looper.getMainLooper().isCurrentThread()
It's practically the same as:
Looper.getMainLooper().getThread() == Thread.currentThread()
but it could be a bit more readable and easier to remember.

- 11,549
- 6
- 42
- 59
public boolean onUIThread() {
return Looper.getMainLooper().isCurrentThread();
}
But it requires API level 23

- 1,619
- 20
- 28

- 347
- 3
- 5
Besides checking looper, if you ever tried to logout thread id in onCreate()
, you could find the UI thread(main thread) id always equals to 1. Therefore
if (Thread.currentThread().getId() == 1) {
// UI thread
}
else {
// other thread
}
-
I cannot find any official documentation that corroborates that this is true, and will always be so. Do you have a link? – intrepidis Jan 10 '14 at 21:33
-
This is what I found in logcat when I want to monitor multi-thread behavior. You can try to output thread id – yushulx Jan 11 '14 at 01:45
-
8I would strongly recommend against this, as this value may be specific to your device and/or version of Android. Even if it were the case on every Android device right now, there is no guarantee that it will continue to be the case in later versions. Saving the thread ID in a class member when running onCreate() seems a little more reasonable to me. – personne3000 Jul 09 '14 at 14:33
Nice extension for Kotlin:
val Thread.isMain get() = Looper.getMainLooper().thread == Thread.currentThread()
So you just call:
Thread.currentThread().isMain

- 5,887
- 1
- 47
- 66
Couldn't you use the runOnUiThread
method in the Activity
class?See..

- 6,274
- 2
- 29
- 60
-
4My application is working, but, it has several authors and is becomming rather big and complex. What I want to do is to add an extra safety net, an assert that catches the misstake if somebody is calling a method that is designed to only be called from the GUI-thread from another thread. – ParDroid May 17 '10 at 12:30
-
I am currently fixing a bug where using runOnUiThread causes the UX to flash. – fobbymaster Dec 13 '16 at 02:12