Interviewer: Source Handler seen it? Please briefly Android message passing between threads?

The interviewer asked the most is the most common:

Source Handler seen it? Please briefly Android message passing between threads?

=

In this case, first of all we must first sort out the answer ideas, then our structured thinking to conquer the interviewer! (Guilty ~)

(Flattering look) I: is this inter-thread messaging major Android can be roughly divided into the following sections: messaging, message loop, message distribution, then I talk about specific implementation logic of these three sections.

1, how the message is sent?

This problem can also be extended: the way to send the message latency is the delay inserted in it, or delay the execution? I also wrote a specific answer on the flow chart, and he summarized it ~

2, how to carry out the message loop?

From here we can know, usually we pass postDelaythe message sent is not precise execution.

Suppose a message execution time is too long, when finished with it again next message processing, it may have been the time to execute the next message in the provision.

3, how to deal with distributing messages?

This process is very simple message, not the drawing. Festival on the code:

Describe in words is this:

1, when msg.callbacknot empty, the callback msg.callback.run()method

2, when handlerthe mCallbackmember variable is not empty, the callback method mCallback.handleMessage(msg), if it returns true, direct end; otherwise, step 3

3, calls Handleritself handleMessage(), that is the way we always reproducible;


other problems

1, Looper Looper and the main thread of the child thread What is the difference?

answer:

  • Looper can exit the sub-thread, the main thread can not exit Looper
  • Sub-thread Looper You'll just have to prepare, to create their own, but the main thread Looper help you create a default

talk is cheap ,上图~

2, MessageQueue is how to create?

A: MesageQueue in calls to the underlying nativemethod to create, in essence, is a single chain;

3, Handler's message latency is how to achieve?

  • Trigger message according to the message queue time (msg.when)ordering
  • native By setting epoll_waitthe timeout to make it wake up at a specific time
  • Delay precision is not high

4, IdleHandler find out about it? You can talk about principle?

About IdleHandlercombined source in the next article, but here everyone to know IdleHandlercan be achieved is a way of execution logic when the main thread of the idle time.

This paper shortage

In fact thread communication also involves the interaction of many native layer, the complete architecture should look like this:

We are interested can go to gityuan Great God of the article, five-star recommendation ~ ~ ~

Of course, the interview when the interviewer asked questions can be more than this one, no matter what the size of the interview, the interviewer order not to be a child not to do, and only maxed face questions questions do thorough preparation, of course, in addition to this usually also need to play their own solid foundation, so no matter how the interviewer a kind of knowledge in Kill chisel, you can cope with such flow ah ~

Attentive I bring to the interview questions Collection

[2017-2019 bytes beating interview Zhenti analytical & resume writing template PDF]

For face questions byte beating finishing, classified, step by step, from basic to in-depth, from easy to Jane.

The content of the discussion into five chapters, basic computer face questions, questions surface data structures and algorithms, Java interview questions, Android interview questions, interview questions other extensions, a total of five non-technical questions section 354.


[2017-2019 bytes beating interview Zhenti analytical & resume writing template PDF] concern I see personal introduction , or direct private letter I receive FREE

Each issue is accompanied by a reference standard answer is trial and error digestion (really spent a lot of time), it feels good to write articles as the answer.

Save time we all go to the search, time spent on the right things. .

Also finishing a full set of resume writing, spring recruit confused, HR interview and other issues to resolve reference suggestions are benefits to others.

Published 168 original articles · won praise 71 · views 20000 +

Guess you like

Origin blog.csdn.net/Aerfa789/article/details/105194293