Gold 9 Silver 10, share several important Android face questions

Source: Internet
Author: User
Tags cas webp tinypng

Explain the understanding of Java polymorphism, and the understanding of inheritance, and interfaces

Hugo here only to talk about polymorphism, other self-surfing experience

for polymorphic definitions
Different classes of objects react or act differently to a uniform function.
Role
The main is to eliminate the coupling between the classes, flexibility is strong, conducive to code writing and modification. In particular, when dealing with a large number of operations and operations, you can flexibly simplify, replace or modify the code!

Three Prerequisites
1, a stool (inherit extends)
2. Rewrite
3. Parent class reference to child class object

For example







Test results:


Test results

The principle of okhttp

1. Synchronous and Asynchronous:

    • 1. Asynchronously uses dispatcher to assign requests stored in Deque to threads in the thread pool for execution.
    • 2. When the task execution completes, regardless of whether there is an exception, the finally code snippet is always executed, that is, the finished function that calls dispatcher, which removes the running task call from the queue runningasynccalls, Proactively took the cache queue a step forward.

2. Connection pool:

    • 1. A connection encapsulates a unit that stores s in a socket,connectionpool and all connection,streamallocation is a reference count
    • 2. When a request gets a connection, pass in a streamallocation,connection with a weak reference to the Streamallocation list, whenever the upper application references a connection, Streamallocation will add one. Conversely, if the upper application is not used, it will delete one.
    • In 3.ConnectionPool, there will be a background task scheduled to clean up the streamallocation list of empty connection. 5 minutes to maintain 5 sockets

3. Select a route and establish a connection

    • 1. There are two ways to choose a route:
      • 1. No proxy, then the local use of DNS lookup to IP, note that the result is an array, that is, a domain name has multiple IPs, this is the source of automatic re-connection
      • 2 Proxy http: Set the IP of the socket as proxy address, set the port of the socket as the proxy address.
      • 3. Agent Benefits: The HTTP proxy will help you to make DNS queries on the remote server, which can reduce the DNS hijacking.
    • 2. Establish a connection
      • 1. There is already a connection in the connection pool, remove the (get) realconnection from it and go to the next step without a hit
      • 2. Based on the route selected (route), call Platform.get (). Connectsocket Choose the best socket library for the current platform runtime handshake
      • 3. Put the successful realconnection into the (put) connection pool cache
      • 4. If TLS is present, secure handshake against the certificate based on the SSL version
      • 5. Construct the Httpstream and maintain the socket connection, the pipeline is established.

4. Responsibility chain mode: Cache, retry, establish connection and other functions exist in the Interceptor network request correlation, mainly is the network request optimization. Problems with network requests

Thread synchronization issues, common thread synchronization

1.SYCN: Guaranteed atomicity, visibility, and ordering
2. Lock: Guaranteed atomicity, Visibility, order

    • 1. Spin lock: Allows a thread to not be suspended while not acquiring a lock, and to perform an empty loop.
      • 1. Advantages: The probability of the thread being suspended is reduced, and the thread execution consistency is enhanced. Used for lock contention is not very intense, the lock takes a short time for concurrent threads.
      • 2. Cons: Excessive waste of CPU time, there is a thread two times in a row trying to get a spin lock causing a deadlock
    • 2. Blocking Lock: The thread that does not get locked waits or hangs, SYCN, lock
    • 3. Can be re-entered lock: A line examples multiple times to obtain the lock, SYCN, lock
    • 4. Pessimistic lock: Every time you go to the data to think that others will change, so will block all other threads SYCN, lock
    • 5. Optimistic Lock: Every time you go to the data to think that others will not be modified, so will not be locked, but in the update will be judged when someone else has to update this data, you can use the version number and other mechanisms. Cas
    • 6. Display lock and built-in lock: Display lock with lock to define, built-in lock with synchronized.
    • 7. Read-write Lock: To improve performance, Java provides read

3.volatile

    • 1. Only visibility is guaranteed and atomicity is not guaranteed
    • 2. There are three steps to the self-increment operation, at this time the problem of multithreading writing

4.cas

    • 1. Operation: Memory value V, old expected value A, the value to be modified B, when and only if the expected value A and the memory value of the same time, the memory value is modified to B and return true, otherwise do nothing and return false.
    • 2. Explanation: The local copy is a, the shared memory is V, and thread A changes V to B. At some point, thread A is going to change V to B, if A and V are different then it means that there are other threads modifying V, which means that the modification fails, otherwise it means that there is no other thread modification, then V is changed to B.
    • 3. Limitation: if V is modified to V1 and then changed to V, then CAS does not recognize the change, or if no other thread is modifying V, there will be a problem at that time
    • 4. Limitation: Put V on the version.

5. What's going on with thread insecurity:

    • 1. A thread writes, when multiple threads read, it causes half of the write to be read
    • 2. Multi-threaded write, can cause dirty data
Asynctask and thread pools, GC-related (how to determine which memory the GC,GC algorithm)

1.Asynctask: Asynchronous task class, single thread thread pool +handler
2. Thread pool:

    • 1.ThreadPoolExecutor: Through executors, you can construct a single-thread pool, a fixed number of thread pools, and a fixed number of threads.

    • 2.ScheduledThreadPoolExecutor: Can delay the calling thread or delay repeating the scheduled thread.
      3.GC Related: Important

    • 1. Search algorithm:
      1. Reference counting
      2. Graph Search, accessibility analysis

    • 2. Recovery algorithm:
      1. Mark clear replication: With the Qinghai age
      2. Labeling: Used in the old age

    • 3. Heap Partitioning:
      1. Eden 80%, Survivor1 10%, Survivor2 10%, Youth Zone
      2. Elderly Area

    • 4. Virtual Machine Stack Partitioning:
      1. Local Variables Table
      2. Operand stacks
      3. Dynamic linking
      4. Method return Address

    • 5.GC Roots:
      1. Referenced objects in the virtual machine stack (local variable table in the stack frame)
      2. Objects referenced by class static properties in the method area
      3. Constants in the method area reference objects
      4. Objects referenced by JNI in the local method stack

Java class loading process:
    • 1. Loading opportunity: Before creating an instance, accessing a static variable or method, reflecting, loading a subclass
    • 2. Validation: Verify the correctness of file format, metadata, bytecode, symbol references
    • 3. Load: Gets the file byte stream according to the full class name, streams the bytes into the static storage structure into the method area, generates the class object
    • 4. Prepare: partition memory on the heap for static variables
    • 5. Parsing: Converting a symbol reference in a constant pool to a direct reference
    • 6. Initialize: Initialize static variables
MVC, MVP, MVVM:
    • 1.MVC: Data, view, Activity,view the operation feedback to activity,activitiy to get the data, the data through the viewer mode to refresh to View. Cyclic dependency
      • 1.Activity Heavy, hard to unit test
      • 2.View and model coupling critical
    • 2.MVP: Data, View, Presenter,view will operate to Presenter,presenter to get the data, the data get good back to Presenter,presenter to refresh the View. PV,PM bidirectional dependency
      • 1. Interface explosion
      • 2.Presenter is heavy.
    • 3.MVVM: Data, View, Viewmodel,view will operate to Viewmodel,viewmodel to get data, data and interface binding, Data update interface update.
      • 1.viewModel of business logic can be tested separately
      • 2. A view corresponds to a ViewModel business logic that can be detached without the presence of the Almighty class
      • 3. Data and interface binding, do not write garbage code, but the reuse of uncomfortable
APK Slimming:

1.classes.dex: Through code obfuscation, delete unnecessary jar packages and code to achieve the optimization of the file
2. Resource files: Scan static resources that are not used in code with the Lint tool
3. Picture resources: Using Tinypng and WEBP, the following details the Picture resource optimization scheme, vector diagram
4.SO files will not be removed, the current mainstream app is generally only one arm's so package

The formation of the ANR, what is the time limit for the ARN on each component

1. As long as the main thread time-consuming operation will be ARN such as IO
2.broadcast Timeout is 10 seconds no response timeout is 5 seconds The foreground service has an unresponsive timeout of 20 seconds and a background service of 200 seconds

The difference between serializable and parcelable

1.P Low Memory consumption
2. Use p in the S Program for network transmission
3.S easy to persist data
4.S use of reflection easy to trigger garbage collection is relatively slow

Sharedpreferences Source Code Brief

1. Stored on the hard disk of the XML key-value pairs, more data will have a performance problem
2.ContextImpl records the important data of sharedpreferences, the file path and the key value pair of the instance.
3. Before the XML file is loaded into memory, the read operation is blocked, and after the XML file is loaded into memory, the data in memory is read directly
4.apply because there is no return value for async, commit is synchronous with a return value to know if the change was committed successfully
5. When a multiple concurrent commit commits, it is necessary to wait for the commit data being processed to be updated to the disk file before continuing execution, thus reducing the efficiency; While apply is only an atom update to memory, invoking the Apply function directly overwrites the previous memory data to a certain extent to improve the efficiency. 3.edit () Creates a new Editorimpl object each time.

The formation of the ANR, what is the time limit for the ARN on each component

1. As long as the main thread time-consuming operation will be ARN such as IO
2.broadcast Timeout is 10 seconds no response timeout is 5 seconds The foreground service has an unresponsive timeout of 20 seconds and a background service of 200 seconds

APK Slimming:

1.classes.dex: Through code obfuscation, delete unnecessary jar packages and code to achieve the optimization of the file
2. Resource files: Scan static resources that are not used in code with the Lint tool
3. Picture resources: Using Tinypng and WEBP, the following details the Picture resource optimization scheme, vector diagram
4.SO files will not be removed, the current mainstream app is generally only one arm's so package

Read more

20+ a great Android Open source project

How did I get into Facebook?

2018 Android face Test with answer-for advanced (bottom)

When you're done, you're going to git. (Teach you to build a small mall with Vue+node+mongodb

Believe in yourself, there is nothing to do, only unexpected

Gold 9 Silver 10, share several important Android face questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.