在主线程中创建的处理程序的行为就像在另一个线程上一样 [英] Handler created in main thread acts like it is on another thread

查看:93
本文介绍了在主线程中创建的处理程序的行为就像在另一个线程上一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如Google所说:

处理程序允许您发送和处理Message和Runnable对象 与线程的MessageQueue相关联.每个处理程序实例为 与单个线程和该线程的消息队列相关联.什么时候 您创建一个新的处理程序,它绑定到的线程/消息队列 创建它的线程-从那时起,它将交付 消息和可运行对象到该消息队列,并在它们执行时执行 从消息队列中出来.

我希望当我在主线程(UI线程)中创建一个Handler时,它将附加到此线程,从而导致冻结ui直到结束其任务.但是在测试中不会发生这种情况,它的行为就像是在backgound线程上并并行执行其任务. 我以前是这样创建Handle的:

 Handler mainHandler = new Handler(Looper.getMainLooper());

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 35000; i++) {
                log.i(TAG,"log in Handler:"+i);
            }}
    };

    mainHandler.post(runnable);
    log.i(TAG,"log outSide");

在mainActivity中(因此句柄应绑定到Main线程).那么,这是什么问题呢?还是我以错误的方式创建了处理程序?

一些朋友注意到doSomthing()还不够复杂,但是如果我们在同一个线程中,为什么在登录处理程序:"之前看到"logoutSide".

解决方案

实际上,它按预期工作,您的处理程序当前与您的主线程相关联,因为它是在主线程中创建的,并且您的任务也在同一线程上运行.尝试在for循环内添加 Thread.sleep(1000)方法,然后您会在UI中看到冻结. 由于您的N是常数,因此您当前的代码以O(1)的复杂度运行,并且您的手机能够在不到一秒钟的时间内运行此代码,这就是为什么您在测试期间未观察到UI冻结的原因.

As google says:

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

And i expect when i create a Handler in main thread (UI thread) it attached to this thread so it cause to freeze ui till end it's task. But in test this not happen and it is acts like it is on a backgound thread and do his task parallel. I used to create Handle like this:

 Handler mainHandler = new Handler(Looper.getMainLooper());

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 35000; i++) {
                log.i(TAG,"log in Handler:"+i);
            }}
    };

    mainHandler.post(runnable);
    log.i(TAG,"log outSide");

In mainActivity (so handle should bound to Main thread). So what is the problem with this or am i create the handler in wrong way?

Some friends notice that doSomthing() is not complicated enough but why we see "log outSide" before "log in Handler:" if they are in a same thread.

解决方案

Actually, It's working as you expected, Your handler is currently associated with your main thread because it is created in it and your task is also running on the same. Try to add Thread.sleep(1000) method inside your for loop then you will see the freeze in your UI. Your current code runs with a complexity of O(1) since your N is constant, and your phone is capable enough to run this in a fraction of a second that's why you are not observing any freeze in your UI during the test.

这篇关于在主线程中创建的处理程序的行为就像在另一个线程上一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆