淘男网推广,每天每夜110404中字,同人漫画h
如果在android中判断某个线程是否是主线程?对于这个问题,你可能说根据线程的名字,当然这个可以解决问题,但是这样是最可靠的么?万一某天google一下子将线程的名字改称其他神马东西呢。
方法揭晓
下面的方法是最可靠的解决方案。
实际上,写到这里就基本解决了文章标题的问题了,但是仅仅研究到这里太肤浅了,刨的不够深,所以需要继续,希望你也可以继续读下去。
刨根问底
实验一
好,现在,我们对这个稳定的方法做一些测试,首先,下面的方法会增加一些调试打印信息。
ok,我们看一下输出日志。验证ok。
实验二
现在我们继续在一个没有消息循环的非主线程,进行验证。
实验三
继续,我们创建一个绑定了消息循环的线程,根据android开发者文档说明,以下是一个典型的创建消息循环线程的示例,使用单独prepare()方法和loop()方法来创建一个绑定到looper的handler。
ok,现在再次检查以下日志,
两个looper都被初始化赋值了,但是他们是不同的对象。
原理发掘
但是,这是为什么呢,这里面有什么奥秘呢? 好,让我们看以下looper.class
/**
* initialize the current thread as a looper, marking it as an
* application's main looper. the main looper for your application
* is created by the android environment, so you should never need
* to call this function yourself. see also: {@link #prepare()}
*/
public static void preparemainlooper() {
prepare(false);
synchronized (looper.class) {
if (smainlooper != null) {
throw new illegalstateexception("the main looper has already been prepared.");
}
smainlooper = mylooper();
}
}
private static void prepare(boolean quitallowed) {
if (sthreadlocal.get() != null) {
throw new runtimeexception("only one looper may be created per thread");
}
sthreadlocal.set(new looper(quitallowed));
}
/**
* return the looper object associated with the current thread.
* returns null if the calling thread is not associated with a looper.
*/
public static looper mylooper() {
return sthreadlocal.get();
}
/** returns the application's main looper, which lives in the main thread of the application.
*/
public static looper getmainlooper() {
synchronized (looper.class) {
return smainlooper;
}
}
对于主线程来说,preparemainlooper这个方法会被android运行环境调用,而不是程序显式调用。通过这个方法,主线程的looper被创建,并且将对象引用传递给smainlooper。所以保证了主线程mylooper()获取到的引用和getmainlooper()获取到的都是同一个引用。
对于没有消息循环的非主线程,默认的当前线程的looper是null,因为你从来没有手动地调用prepare(),所以它和主线程的looper不一样。
对于绑定了消息循环的非主线程,当调用looper.prepare方法时,主线程的looper已经由android运行环境创建,当调用prepare方法后,绑定到这个非主线程的looper被创建,当然,这不可能和主线程的looper一样。
综上所述,这个方法是可靠的。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论