Android面试题:Handler

Q1:消息机制Hander是什么?有什么作用?有哪些要素?流程是怎样的?
Q2:为什么系统不建议在子线程访问UI?
Q3:一个Thread可以有几个Looper?几个Handler?
Q4:如何将一个Thread线程变成Looper线程?Looper线程有哪些特点?
Q5:可以在子线程直接new一个Handler吗?那该怎么做?
Q6:Message可以如何创建?哪种效果更好,为什么?
Q7:这里的ThreadLocal有什么作用?
Q8:主线程中Looper的轮询死循环为何没有阻塞主线程?
Q9:使用Hanlder的postDealy()后消息队列会发生什么变化?

相关文章:要点提炼|开发艺术之消息机制

相关视频:Android面试常客Handler详解(4-2,07:41 Source Insight可以很方便查看Android源码)

Q1:消息机制Hander是什么?有什么作用?为什么要使用Handler?

(1)、Handler是什么?

Handler是Android给我们提供用来更新UI的一套机制,也是一套消息处理的机制,我们可以发送消息,也可以通过它处理消息。

(2)、Handler的原理是什么?


一、Handler封装了消息的发送(主要包括消息发送给谁,以及如何去发送。默认情况下消息都是发送给Handler自己)。
二、Looper:轮询,消息封装的一个载体,
1、Looper内部包含一个消息队列也就是MessageQueue,所有的Handler发送的消息都走向这个消息队列;
2、Looper.looper方法,就是一个死循环,不断地从MessageQueue取消息,如有消息就处理,没有就阻塞;
三、MessageQueue,就是一个消息队列,可以添加消息,并处理消息。
四、Handler也很简单,内部会跟Looper进行关联,也就是说在Handler的内部可以找到Looper,找到了Looper也就找到了MessageQueue,在Handler中发送消息,其实就是向MessageQueue队列中发送消息。
总结:Handler负责发送消息,Looper负责接收Handler发送的消息,并直接把消息回传给Handler自己,MessageQueue就是一个存储消息的容器。

(3)、Handler有什么作用?

  • 更新UI
  • 发送消息
  • 处理消息

(4)、为什么要使用Handler?不用行不行?

  • 是不行的
  • Android在设计的时候,就封装了一套消息创建、传递、处理机制,如果不遵循这样的机制,就没有办法更新UI信息,就会抛出异常信息。

(5)、android为什么要设计只能通过Handler机制更新UI呢?


最根本的目的就是解决多线程并发问题。
假如在一个Activity当中,有多个线程去更新UI,并且都没有加锁机制,那么会产生什么样子的问题?
答案是:更新界面错乱。
如果对更新UI的操作都进行加锁处理的话又会产生什么样子的问题?
答案是:性能下降。
出于对以上问题的考虑,android给我们提供了一套更新UI的机制,我们只需要遵循这样的机制就行了,而根本不用去关心多线程问题,所有的更新UI的操作,都是在主线程的消息队列当中去轮询处理的。

(6)、Handler怎么用?

  • sendMessage
  • sendMessageDelayed
  • post(Runnable)
  • postDealyed(Runnable,long)

(7)、android中更新UI的几种方式:

  • runOnUIThread
  • handler post 
  • handler sendMessage
  • view post

(8)、使用Handler时候遇到的问题?

  •    android.view.ViewRootImpl$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views.   只有原生线程才能更新UI。
  •     Can't create handler inside thread that has not called Looper.prepare().

(9)、谷歌官方开发文档中关于对Handler的介绍:

A Handler allows you to send and process Message and Runable 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 message and
runnables to that message queue and execute them as they come out of the message queue.
翻译:一个Handler允许你发送和处理跟一个Thread的MessageQueue相关的Message和Runable对象。每个Handler实例都跟一个单独的Thread以及该Thread创建的MessageQueue相关。当你创建一个Handler的时候,这个Handler就跟一个Thread以及该Thread创建的MessageQueue绑定在一起,所以,这个Handler将会传递messages和runnables给那个MessageQueue,并且处理来自这个MessageQueue的messages和runnables。
解释:我们创建一个Handler的时候,它会跟一个默认的Thread绑定,这个默认的Thread中就有一个MessageQueue(消息队列)。

When a process is created for your application, its main thread is dedicated(专用于) to running a message queue that takes care of managing the top-level application objects(activities,broadcast receivers .etc) and any windows they create.You can create you own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessge methods as before,but from your new thread. The given Runnable or Message will then be scheduled(列入计划表) in the Handler's message queue and processed when appropriate(适当的时候).

当我们创建一个application的时候,它就会自动创建一个process(进程),这个process的main thread(即application的主线程,也是UI线程)是一个ActivityThread,main thread会默认创建一个Looper,这个Looper就和主线程及其MessageQueue产生了联系。这个main thread 专门用来运行一个message queue,这个message queue用于管理顶级的application对象(例如activities,broadcast receivers等)和它们创建的windows。你也可以创建自己的threads,这些threads可以通过Handler的post or sendMessge methods跟main thread联系。给定的Runnable or Message将会被放入message queue ,并且在合适的时候会被处理。

猜你喜欢

转载自blog.csdn.net/songzi1228/article/details/82835982