"Golden Nine Silver Ten" autumn recruitment season, please accept this set of Android interview questions + answer analysis for major Internet companies

Golden September and Silver October, every year in September and October, major Internet companies will undergo periodic personnel changes. Whether it is a rookie in the workplace who has just entered the society or a veteran who is about to change jobs, they all want to get a new job at this time, or usher in a promotion. best opportunity for salary.

What is different from previous years is that this year's Internet winter seems to be a bit colder and the situation is more severe. Many companies are laying off employees, and a large number of people may have experienced layoffs during job hunting. Even under such circumstances, there will still be a wave of resignation and job hunting in September and October.

As a job seeker, the interview is a hurdle. Many people will be afraid of interviews. Even programmers who have worked for many years may still have interview anxiety.

Aiming at the interview job-hopping season of "Golden September and Silver October", today I will share with you this set of interview questions for Zhongdachang, including previous years and the latest interview questions and precautions. When reading, it is recommended not to read the answers first, but The best results can be achieved by thinking about it yourself first, and then looking at the reference answers.

1. In the Java exception mechanism, the difference between exception Exception and error Error

ThrowableThere is a throwable class in Java Throwablewith two important subclasses, one is Error and the other is Exception.

insert image description here

ErrorIt is an error that the program cannot handle, indicating a serious problem in the program. For example, the Java virtual machine runs wrong (Virtual MachineError), when the JVM no longer has the memory resources needed to continue the operation, OutOfMemoryError will appear, and so on. When these errors occur, the JVM will generally choose to terminate the thread. These errors cannot be checked, they are outside the control and processing capabilities of the program, and most of them are not allowed to occur when the program is running.

ExceptionIt is an exception that the program can handle. And Exception is divided into runtime exception (RuntimeException) and non-runtime exception.

  • Abnormal operation

    Runtime exceptions, also known as unchecked exceptions . The so-called non-inspection means that Java will not tell us that there is an exception during compilation and inspection, and it needs to be exposed at runtime, such as subscript out of bounds, null pointer exception, etc.

  • non-runtime exception

    Exceptions other than RuntimeException are collectively referred to as non- runtime exceptions , such as IOException and SQLException, which are exceptions that must be handled ( check exceptions ). If they are not handled (throw to the upper layer or try-catch), the program cannot be compiled.

Second, talk about your understanding of Activity.runOnUiThread?

Generally, it is used to bind a runnable to the main thread. In the source code of runOnUiThread, it will judge whether the current runnable is the main thread. If it is run directly, if not, post the runnable into the looper through a default empty constructor handler, and create The constructor handler will bind a looper object of the main thread by default

3. Can the child thread update the UI? Why?

The child thread cannot directly update the UI.
Note this sentence, it cannot be updated directly, it is not impossible to update (updatable in extreme cases), the drawing process must be kept in sync (otherwise the page is not smooth), and our main thread is responsible for drawing the UI, extreme The situation is that the sub-threads can update the ui in the life cycle before (including) the onResume of the Activity, that is, onCreate, onStart and onResume, and the drawing of the main thread has not started yet.

Fourth, talk about the Handler mechanism and principle?

First, we create a Handler instance object in the UI thread. Whether it is an anonymous inner class or a Handler instance object generated by a custom class, we need to rewrite the handleMessage method. In the handleMessage method, we can write the acceptance message through the parameter msg After the logical processing of the UIi thread, we create a sub-thread. When the UI needs to be updated in the sub-thread, a new Message object is created and the data of the message is recorded inside the message object Message, such as arg1, arg2, obj, etc. Then call the sendMessge method through the previous Handler instance object to send the Message instance object, and then the message will be stored in the MessageQueue to be processed. Call back the dispatchMessage method to pass the message to the Handler's handleMessage method, and finally the aforementioned message will be taken out of the MessageQueue by the Looper and passed to the handleMessage method.

5. Why doesn't Zygote use Binder mechanism for IPC communication?

There is a Binder thread pool in the Binder mechanism, which is multi-threaded. If Zygote uses Binder, there will be problems with fork() and multi-threading mentioned above. In fact, strictly speaking, the Binder mechanism does not necessarily have to be multi-threaded. The so-called Binder thread is just reading the messages driven by the Binder in a loop. It is also possible to register only one Binder thread, such as the service manager. In fact, although Zygote does not adopt the Binder mechanism, it is not single-threaded, but it actively stops other threads before fork(), and restarts after fork().

6. What are the advantages of Binder

performance

  • Shared memory 0 data copy
  • Binder 1 data copy
  • Socket/Pipeline/Message Queue 2 data copies

Stability

  • Binder: Based on the C/S architecture, if the client (Client) has any needs, it will be thrown to the server (Server) to complete. The structure is clear, the responsibilities are clear and independent of each other, and the natural stability is better
  • Shared memory: Although no copy is required, the control is complex and difficult to use
  • From a stability point of view, the Binder mechanism is superior to memory sharing.

security

  • The traditional IPC does not have any security measures, and the security depends on the upper layer protocol to ensure.
  • The traditional IPC method cannot obtain the reliable process user ID/process UI (UID/PID) of the other party, so that the identity of the other party cannot be identified.
  • Traditional IPC can only be filled with UID/PID in the data packet by the user, which is easy to be used by malicious programs.
  • Traditional IPC access points are open, which cannot prevent malicious programs from obtaining connections by guessing the receiver's address.
  • Binder supports both real-name binder and anonymous binder, with high security.

7. How does Binder make a copy

The main reason is that Linux uses virtual memory addressing mode, which has the following characteristics:

  • Virtual memory addresses in user space are mapped to physical memory.
  • Reading and writing to virtual memory is actually reading and writing to physical memory. This process is memory mapping.
  • This memory mapping process is implemented through the system call mmap().
  • Binder uses the method of memory mapping to make a layer of memory mapping between the data buffer areas of the receiver user control in the kernel space, which is equivalent to directly copying to the data buffer area of ​​the receiver user space, thereby reducing a data copy .

Eight, the relationship between Widget and element and RenderObject

First, let me describe the situation in detail. The interviewer asked me if there is a one-to-many relationship between Widget and Element, and what is the relationship after adding a Widget. There is still no good answer to this part. It is just a guess now. If a widget is added, the Element tree traverses all the subsequent Elements to see if the type has changed, and if so, rebuild the RenderObject. There should still be a one-to-one relationship between Element and Widget, because the context of each Widget is unique.

9. What is Flutter?

Flutter is Google's mobile UI framework that can quickly build high-quality native user interfaces on iOS and Android. Flutter works with existing code. All over the world, Flutter is being used by more and more developers and organizations, and Flutter is completely free and open source.

10. What are the features of Flutter?

  1. Rapid development (millisecond-level hot reload)
  2. Gorgeous UI (built-in beautiful texture design Material Design and Cupertino Widget and rich and smooth animation effects and platform awareness)
  3. Reactive (Reactive, using a powerful and flexible API to solve problems such as 2D, animation, gestures, effects, etc.)
  4. native access function
  5. Comparable to native performance

Eleven, the life cycle in Flutter

initState() indicates that the current State will be associated with a BuildContext, but the BuildContext has not been fully loaded at this time. If you need to get the BuildContext in this method, you can use new Future.delayed(const Duration(seconds: 0, (){// context}); click.

didChangeDependencies() is called after initState(). This method is called when the dependencies of the State object change, and it is also called during initialization. deactivate() This method will be called when the State is temporarily removed from the view tree, and it will also be called when the page is switched. dispose() Widget is destroyed, always call deactivate() before calling this method. didUpdateWidge is called when the state of the widget changes.

Through StreamBuilder and FutureBuilder, we can quickly use Stream and Future to quickly build our asynchronous controls. The runApp startup entry in Flutter is actually a WidgetsFlutterBinding, which is mainly through BindingBase subclasses GestureBinding, ServicesBinding, SchedulerBinding, PaintingBinding, SemanticsBinding, RendererBinding, Widget sBinding etc. , formed by a combination of mixins.
Dart threads in Flutter exist in the form of event loops and message queues, including two task queues, one is the microtask internal queue, the other is the event external queue, and the priority of microtask is higher than event. Because the priority of microtask is higher than that of event, and it will block the event queue at the same time, if there are too many microtasks, external events such as touch and drawing may be blocked.

There are four major threads in Flutter, namely UI Runner, GPU Runner, IO Runner, and Platform Runner (native main thread). At the same time, in Flutter, real cross-thread asynchronous operations can be performed through isolate or compute.

12. What are the two subscription modes of Stream?

Stream has two subscription modes: single subscription (single) and multi-subscription (broadcast).
Single subscription means that there can only be one subscriber, while broadcast means that there can be multiple subscribers. This is somewhat similar to the processing mode of Message Service. Single subscription is similar to peer-to-peer, it will hold data before the subscriber appears, and hand it over to it after the subscriber appears. Broadcasting is similar to the publish-subscribe mode. There can be multiple subscribers at the same time. When there is data, it will be delivered to all subscribers, regardless of whether there are currently existing subscribers.
Stream is in single-subscription mode by default, so listen and most other methods on the same stream can only be called once, and an error will be reported if the second call is made. But Stream can be called continuously through transform() method (which returns another Stream). Through Stream.asBroadcastStream(), a single-subscription-mode Stream can be converted into a multi-subscription-mode Stream, and the isBroadcast attribute can determine the current mode of the Stream.

13. Briefly describe the overall principle of the Handler mechanism

  1. Looper prepares and starts the round robin: Looper#prepare() initializes the thread-specific Looper and MessageQueue Looper#loop() starts an infinite loop to read the next Message in the MessageQueue that meets the execution time. If there is no Message yet, call pollOnce( on the Native
    side ) enters infinite waiting and if there is a Message, but the execution time when has not been satisfied, the
    remaining time parameter is passed in when pollOnce() is called to enter limited waiting

  2. Message sending, enqueue and dequeue: If the Native side is waiting infinitely: After any thread sends a Message or Runnable to the Handler, the Message will be inserted into the appropriate MessageQueue corresponding to the Looper instance held by the Handler according to the order of when conditions Location.
    MessageQueue will call the wake() on the Native side to wake up the infinitely waiting thread after finding a suitable Message to insert .
    This will prompt the reading of MessageQueue to continue into the next cycle. At this moment , the Message that already meets the conditions in the Queue will be dequeued and returned to the Looper Native side. If it is in a limited wait: epoll_wait
    will return after waiting for a specified period of time. The thread continues to read the MessageQueue. At this moment, because the duration condition will be satisfied, it will be dequeued. Looper processes the implementation of the Message:
  3. After Looper gets the Message, it calls back the callback property of the Message, which is Runnable, or executes the callback of the Handler according to the target property, namely Handler. If the mCallback property exists, call back Handler$Callback; otherwise, call back
    handleMessage()

14. Where does Looper exist? How can you guarantee that the thread is unique?

The Looper instance is managed in the static attribute sThreadLocal, and the ThreadLocal internally holds the Looper through the ThreadLocalMap. The key is the ThreadLocal instance itself, and the value is the Looper instance. Each Thread has its own ThreadLocalMap, which ensures that each thread corresponds to an independent Looper instance. , and then ensure that myLooper() can obtain the unique Looper egg of the thread: How many Looper instances does an App have? How many ThreadLocal instances? How many MessageQueue instances? How many Message instances? Several Handler instances, one thread, only one Looper instance, and one Looper instance corresponds to only one MessageQueue instance. One MessageQueue instance can correspond to multiple Message instances, which are obtained from the Message static pool. There is an upper limit of 50. One thread can have multiple Handler instances. Its Handler is only the entry and exit ThreadLocal instance for sending and executing task logic is static, and the entire process shares one instance. The ThreadLocalMap stored in each Looper weakly references it as a key

15. How to understand the role of ThreadLocal?

First of all, it must be clear that it is not used to switch threads, but just to make it easier for each thread to obtain its own Looper instance. See Looper#myLooper(), which can be used to specify the Looper thread it belongs to when the Handler is initialized. It can also be used by the thread to judge whether it is itself. is the main thread

16. What is ActivityManagerService? When was it initialized? what's the effect?

ActivityManagerService is mainly responsible for the startup, switching, scheduling of the four major components in the system, and the management and scheduling of application processes. Its responsibilities are similar to the process management and scheduling modules in the operating system. The timing of ActivityManagerService initialization is very clear, that is, when the SystemServer process is started, ActivityManagerService will be initialized. (System startup process) If you open an App, you need AMS to notify the zygote process, and the life cycle of all activities is controlled by AMS

Due to the limited space of the article, all the complete interview questions cannot be displayed. Friends who need complete interview questions and answer analysis can scan the QR code below to get it for free

insert image description here

at last

The interview is a process with both challenges and opportunities, no matter what the result is, it should be regarded as a valuable experience.

Guess you like

Origin blog.csdn.net/weixin_43440181/article/details/132381979