2019 mid-level Android developer interview answers to the Handler

Note: Because the actual development with reference to the answer will be different. Furthermore afraid mislead you, so they still face questions answer themselves to understand! Deep knowledge questions for the interviewer will resume point mentioned, so do not answer back, more understanding.

Handler

1. messaging Handler talk about the role? What are the elements? What the process is like?

Reference answer:

Responsible for inter-thread communication, this is because the main thread can not do time-consuming operation, and the child can not update the UI thread, so when the child thread needs to be updated when the UI, the UI related to the operation of the switch by Handler to the main thread after consuming operations in execution.

Specifically divided into four elements
  • Message (message): message needs to be transmitted, the message is divided into hardware-generated message message (e.g., buttons, touch) and software generated.
  • MessageQueue (Message Queue): responsible for storing and managing messages, responsible for managing the Message is sent over by the Handler. Read automatically delete messages, maintain a single list, you have an advantage on the inserts and deletes. Will be an infinite loop in its next () method, and continue to determine whether there is a message, the message is returned there and removed
  • Handler (message processor): Message is responsible for processing and transmission. The main pool of various messages transmitted to the event message (Handler.sendMessage ()) and processing the corresponding event message (Handler.handleMessage ()), performed according to the FIFO, the internal structure of a single list.
  • Looper (news pool): responsible for the associated thread and message distribution, get in the thread from MessageQueue Message, distributed Handler, when Looper creation will create a MessageQueue, call loop () method when the message loop start, which will continue to messageQueue call the next () method, when there is news on the treatment, or obstruction in the next messageQueue () method. When Looper's quit () is invoked calls quit messageQueue of (), this time next () returns null, then loop () method will follow the exit.
Specific procedures are as follows

2019 mid-level Android developer interview answers to the Handler

  • When the main thread creation will create a Looper, but also in the creation of a message queue inside Looper. When taken out of the record key Handler Looper current thread, and obtained by the message queue Looper object, and then add a Message Handler in the message queue by the child thread MessageQueue.enqueueMessage.
  • Open loop through Looper.loop message () continuously polls the call MessageQueue.next (), to obtain the corresponding Message Handler and passed through to Handler.dispatchMessage, Handler.handlerMessage final call processing message.

2. A thread can create a correspondence between multiple Handler, Handler with Looper?

Reference answer:

  • A Thread can have a Looper, a MessageQueen, can have multiple Handler
  • With a thread basis, their magnitude relationship is: Thread (1): Looper (1): MessageQueue (1): Handler (N)

3. Soft references cited differences with weak

Reference answer:

  • Soft references (SoftReference): If an object has only soft references, then when sufficient memory space, the garbage collector does not reclaim it; if the memory space is insufficient, it will reclaim the memory of these objects. As long as the garbage collector does not reclaim it, the object may have been used by the program.
  • Weak references (WeakReference): If an object has only weak references, then the garbage collector thread scanning process, once the objects found only a weak reference, regardless of the current memory space is sufficient or not, will reclaim its memory.
  • The fundamental difference between the two is that: only a weak reference objects have more short life cycle, may be recovered at any time. The object is only a soft reference only when the time was only enough memory recovery, in memory enough, are generally not recovered.

2019 mid-level Android developer interview answers to the Handler

The reason 4.Handler memory leaks caused and the best solution

Reference answer:
leak reason

  • Handler 允许我们发送延时消息,如果在延时期间用户关闭了 Activity,那么该 Activity 会泄露。 这个泄露是因为 Message 会持有 Handler,而又因为 Java 的特性,内部类会持有外部类,使得 Activity 会被 Handler 持有,这样最终就导致 Activity 泄露。
    解决方案:
  • 将 Handler 定义成静态的内部类,在内部持有Activity的弱引用,并在Acitivity的onDestroy()中调用handler.removeCallbacksAndMessages(null)及时移除所有消息。

5.为什么系统不建议在子线程访问UI?

参考回答:
Android的UI控件不是线程安全的,如果在多线程中并发访问可能会导致UI控件处于不可预期的状态

这时你可能会问为何系统不对UI控件的访问加上锁机制呢?因为

  • 加锁机制会让UI访问逻辑变的复杂
  • 加锁机制会降低UI的访问效率,因为加锁会阻塞某些线程的执行

2019 mid-level Android developer interview answers to the Handler

6.Looper死循环为什么不会导致应用卡死?

参考回答:

  • 主线程的主要方法就是消息循环,一旦退出消息循环,那么你的应用也就退出了,Looer.loop()方法可能会引起主线程的阻塞,但只要它的消息循环没有被阻塞,能一直处理事件就不会产生ANR异常。
  • 造成ANR的不是主线程阻塞,而是主线程的Looper消息处理过程发生了任务阻塞,无法响应手势操作,不能及时刷新UI。
  • 阻塞与程序无响应没有必然关系,虽然主线程在没有消息可处理的时候是阻塞的,但是只要保证有消息的时候能够立刻处理,程序是不会无响应的。

7.使用Handler的postDealy后消息队列会有什么变化?

Reference answer:
If only this message, then the message will not be sent in the queue, but when computing time to wake up to, first blocking Looper, time to wake up to it. However, if at this time to join a new message, the message queue head with a longer delay time compared to the head insert, sorted by time trigger, the minimum time of the first team, the maximum time of the tail

8. Can a child thread Handler in new direct it? How to do it?

Reference answer:

No, because the main thread, comprising an inner Activity Looper object, it automatically manages Looper, message processing sent from the child thread. For the child thread, there is no any objects help us maintain Looper object, so we need to manually maintain. So to turn Handler created the first child thread Looper, Looper and open loop

2019 mid-level Android developer interview answers to the Handler

How 9.Message can create? Which is better and why?

Reference answer: You can create three ways:

  • Examples of direct generation Message m = new Message
  • 通过Message m = Message.obtain
  • 通过Message m = mHandler.obtainMessage()

After both the better, because the number of messages in the message pool Android default is 10, the latter two is to take a message Message instance in the pool, this can avoid generating multiple Message instance.

Guess you like

Origin blog.51cto.com/14332859/2415831