Let’s talk about Java polymorphism, inheritance, and interfaces
Elder brother here only speak polymorphisms, other own experience online
For the definition of polymorphism, objects of different classes have different responses or actions to unified functions. The main function is to eliminate the coupling between classes, flexibility is relatively strong, conducive to code writing and modification. Especially when dealing with a large number of operations and operations, the flexibility to simplify, replace or modify the code!
Three requirements 1. A stool (extends) 2. Overriding 3
for example
Test results:
The principle of oKhttp
1. Synchronous and asynchronous:
- 1. Asynchrony uses the Dispatcher to dispatch requests stored in the Deque to individual threads in the thread pool.
- 2. When the task is finished, finally will be executed with or without exceptions. The Finished function of the Dispatcher will be called, which will remove the running task Call from the runningAsyncCalls queue and move the cache queue forward.
2. Connection pool:
- A Connection encapsulates a socket. A ConnectionPool stores all connections. StreamAllocation is a unit of reference count
- 2. When a request obtains a Connection, a StreamAllocation is passed in. The Connection contains a list of StreamAllocation weak references. StreamAllocation will add one. If the upper-layer application is no longer in use, it will delete one.
- 3. A background task will periodically clear connections whose StreamAllocation list is empty in the ConnectionPool. Five minutes to maintain five sockets
3. Select routes and establish connections
- 1. There are two ways to choose a route:
- 1. If there is no proxy, the local DNS is used to find the IP address. Notice that the result is an array, that is, a domain name has multiple IP addresses, which is the source of automatic reconnection
- 2. Proxy HTTP: Set the SOCKET IP to the proxy IP address and the socket port to the proxy IP address
- 3. Proxy benefits: HTTP proxy will help you perform DNS queries on remote servers, which can reduce DNS hijacking.
- 2. Establish a connection
- 1. If a RealConnection exists in the connection pool, get the RealConnection from it. If not, go to the next step
- 2. According to the selected Route, call platform.get ().connectsocket to select the best socket library under the current Platform Runtime for handshake
- 3. Put the established RealConnection into the connection pool cache
- 4. If TLS exists, the SSL version is used to secure the handshake with the certificate
- 5. Construct the HttpStream and maintain the socket connection
**4. Chain of responsibility mode: ** Caching, retry, connection establishment and other functions exist in the interceptor network request related, mainly network request optimization. Problems encountered during network requests
Thread synchronization problem, commonly used thread synchronization
Sycn: ensures atomicity, visibility and order. 2. Lock: ensures atomicity, visibility and order
- 1. Spin lock: allows a thread to execute an empty loop instead of being suspended when it has not acquired the lock.
- 1. Advantages: Threads are less likely to be suspended and thread execution is more consistent. Used for concurrent threads where lock contention is not intense and lock occupancy is short.
- 2. Disadvantages: excessive waste of CPU time, a thread trying to acquire a spin lock twice in a row causes deadlock
- 2. Blocking Lock: the thread that does not acquire the Lock waits or suspends, Sycn, Lock
- 3. Reentrant Lock: a thread can obtain the Lock multiple times, such as Sycn and Lock
- 4. Pessimistic locking: Every time you try to get data, you think someone else will modify it, so it blocks all other threads Sycn and Lock
- 5. Optimistic lock: Every time I go to get the data, I think that others will not modify it, so I will not lock it. But when I update the data, I will judge whether others have updated the data during this period, and I can use the version number and other mechanisms. cas
- 6. Display Lock and built-in Lock: display Lock is defined by Lock, and built-in Lock is synchronized.
- 7. Read-write locking: To improve performance, Java provides reads
3.volatile
- 1. Visibility is guaranteed, not atomicity
- 2. There are three steps in the increment operation
4.cas
- 1. Operations: memory value V, old expected value A, value B to be modified, change memory value to B and return true if and only if expected value A and memory value V are the same, otherwise do nothing and return false.
- 2. The local copy is A, the shared memory is V, and thread A needs to change V to B. At some point, thread A needs to change V to B. If A and V are different, it means that another thread is modifying V, and the modification fails. Otherwise, it means that no other thread is modifying V, so change V to B.
- 3. Limitation: If V is changed to V1 and then changed to V, CAS cannot recognize the change and still thinks that no other thread is modifying V, then there will be a problem
- 4. Limitations resolved: add V to version.
5. What is thread insecurity?
- 1. If one thread writes data and multiple threads read data, the data will be read in half
- 2. Multi-threaded data writing may cause dirty data
Asynctask is related to thread pool, GC (how to determine which memory is GC, GC algorithm)
1.Asynctask: Asynctask class, single-thread thread pool +Handler 2. The thread pool:
-
1.ThreadPoolExecutor: You can construct a single thread pool, a fixed number of thread pools, or an unfixed number of thread pools by running Executors.
-
2. ScheduledThreadPoolExecutor: repeat scheduling threads can delay the calling thread or delay. 3.GC related: Important
-
Search algorithm: 1. Reference counting 2. Graph search, accessibility analysis
-
2. Recycling algorithm: 1. Mark clearing copy: used for the young generation 2
-
3. Heap partition: 1. Eden 80%, Survivor1 10%, Survivor2 10% 2. Old district
-
Partition of vm stack: 1. Local variable table 2. Operand stack 3. Method return address
-
5.GC Roots: 1. Object 2 referenced in virtual machine stack (local variable table in stack frame). Object referenced by class static properties in the method area 3. Object referenced by constants in the method area 4. The object referenced by JNI in the local method stack
Java class loading process:
- 1. Load time: before instance creation, static variable or method access, reflection, and subclass loading
- 2. Verification: Verify the correctness of file format, metadata, bytecode, and symbol reference
- 3. Load: get the file byte stream according to the full class name, put the byte stream into the method area as static storage structure, and generate the class object
- 4. Preparation: Partition memory on the heap for static variables
- 5. Parse: Convert symbolic references in the constant pool to direct references
- 6. Initialization: Initialize static variables
MVC, MVP, MVVM:
- 1. MVC: data, View and Activity. The View feeds back operations to the Activity and Activitiy to get data, and the data is refreshed to the View in observer mode. Circular dependencies
- 1. The Activity is heavy and difficult to unit test
- 2. Serious coupling between View and Model
- 2. MVP: Data, View, Presenter. The View gives operations to the Presenter, the Presenter gets the data, the data gets back to the Presenter, the Presenter refreshes the View. PV, PM bidirectional dependence
- 1. The interface explodes
- 2. The Presenter is very heavy
- 3. MVVM: Data, View, ViewModel, View will operate to ViewModel, ViewModel to get data, data and interface binding, data update interface update.
- 1. The business logic of the viewModel can be tested separately
- 2. A View for a viewModel business logic can be separated, there will be no omnipotent class
- 3. Data and interface binding, no need to write garbage code, but reuse uncomfortable
Apk thin body:
1. Classes. dex: Optimize this file by obfuscating the code and removing unnecessary jars and code 2. Resource files: Use the Lint tool to scan for static resources 3 that are not used in your code. Picture resources: Tinypng and webP are used. The following is a detailed description of the picture resource optimization scheme. The vector picture 4.so file will be removed
The formation of ANR, what is the time limit for the occurrence of ARN on each component
ARN is performed for any operation that takes time on the main thread, such as IO. 2. Broadcast timeout is 10 seconds
Difference between Serializable and Parcelable
1. Small memory consumption of P 2. Network transmission with S program using P 3
Sharedpreferences
XML key-value pairs stored on the hard disk will cause performance problems.ContextImpl records important data about SharedPreferences, file path, and instance key-value pairs. The read operation blocks until the XML file is fully loaded into memory. After the XML file is fully loaded into memory, the data is directly read from memory. 4. In the case of multiple concurrent COMMIT, the commit data being processed must be updated to disk files before the execution continues, which reduces the efficiency. However, apply is only atomic update to memory, and the subsequent call to apply directly overwrites the previous memory data, thus improving efficiency to a certain extent. 3. Edit () creates a new EditorImpl object each time.
The formation of ANR, what is the time limit for the occurrence of ARN on each component
ARN is performed for any operation that takes time on the main thread, such as IO. 2. Broadcast timeout is 10 seconds
Apk thin body:
1. Classes. dex: Optimize this file by obfuscating the code and removing unnecessary jars and code 2. Resource files: Use the Lint tool to scan for static resources 3 that are not used in your code. Picture resources: Tinypng and webP are used. The following is a detailed description of the picture resource optimization scheme. The vector picture 4.so file will be removed
To read more
20+ great Android open source projects
How did I get into Facebook?
Android 2018 Interview questions with Answers — Suitable for intermediate and advanced level (II)
Use vue+node+mongodb to build a small mall