android 判断是否在主线程的方法

核心方法如下:

 public class ThreadUtils {

    public static final String TAG = "ThreadUtils";

    public static boolean isInMainThread() {
        Looper myLooper = Looper.myLooper();
        Looper mainLooper = Looper.getMainLooper();
        Log.i(TAG, "isInMainThread myLooper=" + myLooper + ";mainLooper=" + mainLooper);
        return myLooper == mainLooper;
    }

}

咱们实际测试一下,看看日志的打印:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.println("isInMainThread()=" + ThreadUtils.isInMainThread());//在这里调用一下方法
        }

打印日志:

10-25 09:57:59.933 8772-8772/com.everyoo.handlerdaemon I/ThreadUtils: isInMainThread myLooper=Looper (main, tid 1) {70cc4e8};mainLooper=Looper (main, tid 1) {70cc4e8}
10-25 09:57:59.933 8772-8772/com.everyoo.handlerdaemon I/System.out: isInMainThread()=true

myLooper=Looper (main, tid 1) {70cc4e8}
mainLooper=Looper (main, tid 1) {70cc4e8}

说明:很显然,当前线程是在主线程,isInMainThread()=true。


如果在主线程开启一个子线程:

 new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("isInChlidThread()=" + ThreadUtils.isInMainThread());

            }
        }).start();

打印日志:

10-25 09:57:59.941 8772-9006/com.everyoo.handlerdaemon I/ThreadUtils: isInMainThread myLooper=null;mainLooper=Looper (main, tid 1) {70cc4e8}
10-25 09:57:59.941 8772-9006/com.everyoo.handlerdaemon I/System.out: isInChlidThread()=false

注意:myLooper=null
mainLooper=Looper (main, tid 1) {70cc4e8}
为什么myLooper=null为空呢?这是因为Android中的线程默认没有一个和它绑定了的消息循环(Threads by default do not have a message loop associated with them. Of course, the method works)


如果初始化Looper对象并且启动,看代码:

 new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();//初始化Looper对象
                System.out.println("isInChlidThread()=" + ThreadUtils.isInMainThread());

                Looper.loop();//启动Looper

            }
        }).start();

打印日志:

10-25 09:57:59.950 8772-9007/com.everyoo.handlerdaemon I/ThreadUtils: isInMainThread myLooper=Looper (Thread-153, tid 153) {9984c2e};mainLooper=Looper (main, tid 1) {70cc4e8}
10-25 09:57:59.950 8772-9007/com.everyoo.handlerdaemon I/System.out: isInChlidThread() isThresdMeod1=false

myLooper=Looper (Thread-153, tid 153) {9984c2e}
mainLooper=Looper (main, tid 1) {70cc4e8}

虽然myLooper不为null了,但是他们却不属于同一个Looper对象。并不是我们的主线程,所以返回false。

我们还有一种更简单方法,直接通过日志就能看出是主线程还是子线程。并且能分析是不是为同一个子线程。看如下代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "onCreate: 我是ui线程");

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "onCreate: 我是子线程");


            }
        }).start();

日志打印:

这里写图片描述

ps:看,红色标记框里的内容。14090 就是我们的主线程号,而14151 就是我们的子线程号。至于是不是同一个子线程,我们通过比对线程号,就能轻松的知道啦!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,线程的超时处理可以通过以下方法实现: 1. 使用 Handler 和 postDelayed() 方法:可以在线程中使用 Handler 来实现超时处理。在需要进行超时判断的地方,使用 postDelayed() 方法延迟一段时间后执行特定的操作。 ```java Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // 在此处执行超时操作 } }, timeoutMillis); // timeoutMillis 为超时时间,单位为毫秒 ``` 2. 使用 Timer 和 TimerTask:使用 Timer 和 TimerTask 组合也可以实现线程的超时处理。 ```java Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // 在此处执行超时操作 } }, timeoutMillis); // timeoutMillis 为超时时间,单位为毫秒 ``` 3. 使用 CountDownLatch:可以使用 CountDownLatch 在线程中进行等待操作,并设置超时时间。 ```java CountDownLatch latch = new CountDownLatch(1); // 等待超时时间为 timeoutMillis 毫秒 if (latch.await(timeoutMillis, TimeUnit.MILLISECONDS)) { // 在此处执行正常完成后的操作 } else { // 在此处执行超时操作 } ``` 需要注意的是,在进行线程的超时处理时,要确保超时操作不会阻塞线程,否则可能导致应用程序无响应。因此,在超时操作中应避免执行耗时操作,或者考虑将耗时操作放在子线程中执行。 另外,超时时间的设置应根据具体情况进行评估和调整,以确保超时时间充足,但又不会过长影响用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值