Android senior engineer interview actual combat, Android developer job-hopping guide to job-hopping salary doubled

Preface

Now that the recruitment season has entered, this article aims to share the interview methods and experience of well-known Internet company interviewers. I hope that reading this article can give programmers a different interview experience and feelings, relax the interview mentality, and actively prepare for the battle!

Interviewer : What are UI threads?

A: It is used to refresh the thread where the UI is located

Interviewer : Talk more

A: The UI is refreshed in a single thread. If multiple threads can refresh the UI, it doesn’t matter whether it is a UI thread or not. The advantage of a single thread is that there is no need to lock everywhere in the UI framework, and thread synchronization is simple and effective to write

Interviewer : This UI thread you mentioned, which thread is it? Is it the main thread?

A: Take Activity as an example. When we finish time-consuming operations asynchronously in Activity, we can call the Activity.runOnUiThread method to refresh the UI and execute it in the UI thread. Then we will know which thread the UI thread is by looking at this method.

public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else {
        action.run();
    }
}

This method will determine whether it is currently in the main thread, or it will be thrown to the main thread for execution through mHandler. Activity in this mHandler is a global variable in time Activity created by no-argument constructor new Handler()to create together.

Because it has no parameters, which thread is used when creating it, and which thread is used by Looper in Handler. Activity is created in the main thread of the application, so the thread where mHandler.post is executed is also the main thread. I just said that in the runOnUiThread method, first determine if it is in the UI thread. When is this mUiThread assigned? The answer is still in the Activity source code.

final void attach(Context context, ...) {
 ...省略无关代码
 mUiThread = Thread.currentThread();
}

In the Activity.attach method, we assign the current thread to mUiThread. What kind of thread is the current thread? It is also the main thread. As for why the creation of Activity and attach are both the main thread, that is another story. Through the previous analysis, we know that for Activity, the UI thread is the main thread

Interviewer : So your conclusion is that the UI thread is the main thread?

A: This is what you said. Remember that you can't go wrong during development, but it's not accurate enough. An exception will be thrown when refreshing the UI in the child thread

ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

The general idea is that only the thread that initially created the View hierarchy can touch the view. This refers to the thread where the ViewRootImpl was created. Strictly speaking, this thread is not necessarily the main thread. At this point, the same conclusion can be obtained by reading the View.post method. So for View, the UI thread is the thread where the ViewRootImpl was created, and the ViewRootImpl corresponding to the DecorView of the Activity is created on the main thread

Interviewer : When was this ViewRootImpl created

A: After the Activity is created, the main thread of the application will call ActivityThread.handleResumeActivity. This method will add the DecorView of the Activity to the WindowManger, which is the ViewRootImpl created at this time

Interviewer : Can the View be refreshed in an asynchronous thread?

A: Just now we said that as long as the thread created by ViewRootImpl can touch the view, and then when WindowManger.addView is created, ViewRootImpl will be created, so we only need to call WindowManger.addView in the child thread. Can be refreshed in this child thread, this child thread is the UI thread of this View.

Interviewer : Okay, let's talk about something else

End of sentence

I summarized some core knowledge points of Android , as well as some of the latest interview questions, knowledge brain maps and video data analysis.

The private messages of the small partners in need [learning] I will share with you for free, and hope that we can go on together in the future. (Thank you for your continued support, if you need it, get it yourself)

You can also receive it by clicking the link directly!

Android learning PDF+architecture video+interview document+source notes

Some information at a glance:

  • 330 pages of PDF Android learning core notes (including 8 sections)

  • Android learning system corresponding video

  • Android advanced system corresponding learning materials

  • Android BAT interview questions (analysis)

Corresponding learning materials**

[External link image is being transferred...(img-n0iPM5Ph-1610954315224)]

  • Android BAT interview questions (analysis)

Guess you like

Origin blog.csdn.net/fanzhang_vip0723/article/details/112783458
Recommended