android 判断当前线程是不是主线程的几种方法

方法一:

public boolean isMainThread() {
    return Looper.getMainLooper() == Looper.myLooper();
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

方法二:

public boolean isMainThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

方法三:

public boolean isMainThread() {
    return Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId();
}
 

PS:有更好的方法欢迎留言!

你可能感兴趣的:(主线程判断方法,线程,android经验分享)