preface

Choose the development of this line, it means that you want to mix well will continue to learn, your technology and salary, position directly linked, progress for the importance of programmers do not repeat, next as experienced, for the majority of peers to share some learning dry goods, I hope to help you

1, the network

Network protocol model

Application layer: handles specific application details HTTP, FTP, DNS

Transport layer: Provides basic end-to-end communication TCP and UDP for two hosts

Network layer: Controls IP addresses such as packet transmission and routing

Link layer: interfaces related to operating system device drivers and nics

TCP is different from UDP

A TCP connection. Reliable; Orderly; Byte oriented stream; Slow; The weight; Full duplex. Suitable for file transfer and browser

  • Full-duplex: USER A sends A message to user B, but user B can also send A message to user A
  • Half-duplex: User A sends A message to user B, but user B cannot send A message to user A

UDP no connection; Unreliable; Disorder; Message oriented; Speed; Light weight; It is suitable for instant messaging and video calls

TCP three-way handshake

A: Can you hear me? B: I can hear you. Can you hear me? A: I can hear you. Let’s go

Both A and B need to make sure that: You hear what I say; I can hear what you’re saying. So you need three handshakes

TCP waved four times

A: I’m done. B: I know. Wait, I may not be finished

B may not be finished after receiving the message that A is finished, so it cannot immediately reply with the end sign. It can only tell A when IT is finished.

POST is different from GET

The Get argument is placed in the URL; The Post argument in the request Body may not be secure because the argument is in the URL

HTTPS

HTTP is hypertext transfer protocol, plaintext transfer; HTTPS encrypts HTTP data using the SSL protocol

HTTP default port 80; HTTPS uses port 443 by default

Advantages: Security disadvantages: time-consuming, SSL certificate charges, encryption power is still limited, but much better than HTTP

Java Basics & Containers & Synchronization & Design patterns

StringBuilder, StringBuffer, +, string. concat link String:

  • StringBuffer threads are safe, StringBuilder threads are not
  • + is actually implemented with StringBuilder, so you can just use + for acyclic bodies, not for loop bodies, because stringBuilders are created frequently
  • StringBuilder < StringBuffer < concat < + StringBuilder < StringBuffer < concat < +

Java generic erase

  • Structure-related generics such as modifier member variables are not erased
  • The container class generics are erased

ArrayList, and LinkedList

ArrayList

Array-based implementation, fast search: O (1), slow add and delete: O (n) The initial capacity is 10, expansion through the System. ArrayCopy method

LinkedList

Based on bidirectional linked list, slow search: O (n), fast add and delete: O (1) encapsulates the call of queue and stack

A HashMap, HashTable

HashMap

  • Based on arrays and linked lists, arrays are the body of a HashMap; Linked lists exist to resolve hash conflicts
  • If hash conflicts occur and the linked list size exceeds the threshold, the capacity will be expanded. JAVA 8 will convert the linked list to a red-black tree to improve performance and allow the key/value to be null

HashTable

  • The data structure is the same as a HashMap
  • Value cannot be null
  • Thread safety

ArrayMap, SparseArray

ArrayMap

1. Implement based on two arrays, one storing hash; One holds key-value pairs. During capacity expansion, only array copy is required and hash table reconstruction is not required. 2. High memory utilization 3. Not suitable for storing large amounts of data because of binary lookup of key (up to 1000)

SparseArray

1. Based on two arrays, int is used as the key. 2. Not suitable for storing large amounts of data because of binary lookup of key (up to 1000)

The volatile keyword

  • Can only be used to modify variables that may be accessed by multiple threads at the same time
  • Equivalent to lightweight synchronized, volatitle ensures order (disables command reordering) and visibility. The latter also guarantees atomicity
  • Variables are located in main memory, and each thread has its own working memory. Variables have a copy in the working memory of its own thread, and the thread directly operates on this copy
  • Changes to variables that are volatile are immediately synchronized to main memory, keeping the variables visible.

Double check singletons, why volatile?

1. The problem that volatile wants to solve is that if you want to use instance in another thread, you find instance! =null, but instance has not been initialized yet

2. Will the instance = newInstance (); Break it down into three sentences. 1. Allocate memory 2. Initialize instance 3

3. Volatile disallows instruction reordering by ensuring that 2 is followed by 3

Wait and sleep.

  • Sleep is a static method of Thread that can be called from anywhere
  • Wait is a member of the Object method, can only be invoked in the synchronized code block, or you will quote IllegalMonitorStateException illegal monitoring abnormal state
  • Sleep does not release the shared resource lock, and wait does

The lock and synchronized

  • Synchronized is a Java keyword and a built-in feature. Lock is an interface
  • Synchronized automatically releases locks; The lock needs to be released manually, so it needs to be written to a try catch block and released in finally
  • Synchronized cannot interrupt wait locks; Lock can be broken
  • Lock improves the efficiency of read/write operations by multiple threads
  • When the resource competition is fierce, the performance of Lock is significantly better than that of synchronized

Reentrant lock

  • Definition: after a lock has been acquired, the next call to the synchronized code block/attempt to acquire the lock does not have to re-apply for the lock, can directly execute the relevant code
  • ReentrantLock and synchronized are both reentrant locks

Fair lock

  • Definition: The thread that waits the longest gets the lock first
  • An unfair lock does not guarantee which thread will acquire the lock. Synchronized is an unfair lock
  • ReentrantLock The ReentrantLock is an unfair lock by default. You can set it to a fair lock

Optimistic locks and pessimistic locks

  • Pessimistic lock: once a thread obtains the lock, other threads will suspend and wait. It is suitable for the scenario with frequent write operations. Synchronized is a pessimistic lock
  • Optimistic lock: If there is no conflict and no lock is added, the data is updated to determine whether the data expires. If the data expires, the data is not updated. This lock applies to scenarios with frequent read operations
  • Optimistic lock CAS: Compare And Swap. When updating data, Compare whether the original value is equal. If the original value is not equal, the data is past And data is not updated
  • Optimistic lock implementation: AtomicInteger, AtomicLong, AtomicBoolean

There are four necessary conditions for deadlock

  • The mutex
  • Possess and wait
  • Do not take
  • Loop waiting for

Principle of synchronized

  • Each object has a monitor lock: monitor, the synchronization block starts with Monitorenter and ends with Motnitorexit
  • Wait/notify is dependent on the monitor monitors, so in the synchronized code block execution will quote IllegalMonitorStateException anomalies

Java Virtual Machine & Memory structure &GC& Class loading & Four references & Dynamic Proxy

JVM

  • Definition: can be understood as a fictional computer, interprets its own bytecode instruction set to map to the local CPU or OS instruction set, the upper layer only needs to focus on the Class file, independent of the operating system, implement cross-platform
  • Kotlin can be interpreted as a Class file, so it can run on the JVM

JVM memory model

  • Java threads communicate with each other through shared memory, with each thread having its own local memory
  • The shared variable is stored in main memory, and the thread makes a copy of the shared variable into local memory
  • The volatile keyword serves the memory model to ensure memory visibility and ordering

JVM memory structure

Thread private:

1. Program counter: records the address of the bytecode instruction being executed. If the Native method is being executed, it is null 2. Virtual machine stack: When executing a method, the data required by the method is stored as a stack frame and pushed onto the stack. Native method stack: Same as virtual machine stack, but for Native methods

Thread sharing:

1. Heap: Store Java instances, GC main area, generation collection GC method will divide the heap into new generation, old generation 2. Method area: store class information, constant pool, static variables and other data

GC

Recycle area: only for heap and method area; Thread private area data is destroyed at the end of the thread without recycling

Recycling type:

1. Objects in the heap

  • The generational collection GC method divides the heap into Cenozoic and old ages
  • New generation: New objects will enter new generation. Objects are reclaimed by a copy algorithm
  • Old age: New large objects and old objects will enter old age. Objects are reclaimed by a mark-sweep algorithm

2. Class information and constant pool in the method area

To determine whether an object is recyclable:

1. Disadvantages of reference counting method: circular reference

2. Reachability analysis definition: Search from GC ROOT, unreachable objects can be reclaimed

GC ROOT

1. Objects referenced in the VM stack/local method stack 2. Objects referenced by constant/static variables in the method area

Four types of references

  • Strong references: will not be collected
  • Soft reference: When memory is insufficient, it will be reclaimed
  • Weak references: they are collected on GC
  • Virtual references: Objects cannot be retrieved from virtual references and can listen for their collection

ClassLoader

Class lifecycle:

1. Load; 2. Validation; 3. Prepare; Analytical; 4. 5. Initialize. 6. Use; 7. Remove

Class loading process:

1. Load: get the binary byte stream of the class; Generate the runtime storage structure of the method area; Generate a Class object in memory. 2. Verify that the Class byte stream meets vm requirements 3. Preparation: initializes static variables 4. resolution: replaces symbolic references in the constant pool with direct references 5. Initialization: Perform static block code, class variable assignment

Class loading timing:

3. Call static variables of the class (except constants put into the constant pool)

Class loader: Responsible for loading class files

Classification:

1. Boot class loader – no parent class loader 2. Extended class loader – inherits from boot class loader 3. System class loader – Inherits from the extended class loader

Parental delegation model:

When a class is loaded, the parent loader is loaded layer by layer, and the load fails

Why are they called parents? Regardless of custom loaders, system class loaders require two layers of online inquiry, hence the name parent

To determine whether the class is the same, the class loader must be the same in addition to the class information

Advantages:

  • To prevent reloading, the parent loader is loaded and there is no need to load
  • Security to prevent tampering with core library classes

Dynamic proxy principle and implementation

  • The InvocationHandler interface that the dynamic proxy class needs to implement
  • Proxy.newProxyInstance, used to dynamically create Proxy objects
  • Retrofit application: Retrofit uses dynamic proxies to generate a dynamic proxy object for each request interface we define to implement the request

4. Android Basics & Performance Optimization &Framwork

Activity Start mode

  • Standard Standard mode

  • SingleTop stack top reuse mode,

  • Push click message interface

  • SingleTask stack reuse mode,

  • Home page

  • SingleInstance singleton pattern, located in a single task stack

  • Dialing interface details:

  • TaskAffinity: taskAffinity, used to specify the task stack name. The default value is the application package name

  • AllowTaskReparenting: Allows task stacks to be moved

View working Principle

  • DecorView (FrameLayout)

  • LinearLayout

  • titlebar

  • Content

  • Call setContentView to set the View

The performTraversals method call to ViewRoot triggers the rendering of the View, which is then called:

  • PerformMeasure: traverses the View measure to measure the size
  • PerformLayout: Traverses the Layout of the View to determine the position
  • PerformDraw: Traverses a View’s draw

Event distribution mechanism

  • Once a MotionEvent is generated, it is passed in the order of Activity -> Window -> decorView -> View. The View is passed in the order of event distribution, relying on three methods:

  • DispatchTouchEvent: Used to dispatch events, is called as soon as the click event is received, and returns a result indicating whether the current event was consumed

  • OnInterceptTouchEvent: Used to determine whether to intercept an event. After the ViewGroup determines to intercept an event, the event sequence will not trigger the onIntercept that calls the ViewGroup

  • OnTouchEvent: Used to handle events, return a result indicating whether the current event was handled, or pass it on to the parent container for processing

  • Details:

  • An event sequence can only be intercepted and consumed by one View

  • View does not have an onIntercept method and calls onTouchEvent directly

  • OnTouchListener has a higher priority than OnTouchEvent and onClickListener has the lowest priority

  • RequestDisallowInterceptTouchEvent can block the parent container onIntercet method call

Windows, WindowManager, WMS, SurfaceFlinger

  • Window: The abstraction does not actually exist, but in the form of a View, implemented through PhoneWindow
  • WindowManager: External access to Windows, internal interaction with WMS is an IPC process
  • WMS: Manages the layout and order of Windows surfaces, running as a system level service in a separate process
  • SurfaceFlinger: Mixes Windows maintained by WMS in a certain order and displays them on the screen

View animation, frame animation and property animation

The View animation:

  • Function object is View, can be defined in XML, XML implementation is recommended to be easier to read
  • Supports four effects: pan, Scale, Rotate and transparency

Animation:

  • AnimationDrawable, easy OOM

Property animation:

  • Can act on any object, can be defined by XML, Android 3 introduced, it is suggested that the code implementation is more flexible
  • ObjectAnimator, ValuetAnimator, and AnimatorSet are included
  • Time interpolator: Calculates the percentage of current property changes based on the percentage of time elapsed
  • Interpolators for uniform speed, acceleration and deceleration are preset in the system
  • Type estimator: Calculates the value of the changed attribute based on the percentage of the current attribute change
  • The system preset integer, floating point, color value and other types of estimators
  • Precautions for use:
  • Avoid using frame animation, easy OOM
  • Stop the animation when the interface is destroyed to avoid memory leaks
  • Enable hardware acceleration to improve animation fluency. Hardware acceleration:
  • Share some CPU work with the GPU, and use the GPU to complete drawing
  • The drawing speed is optimized from two aspects of work allocation and drawing mechanism

Write in the last

In the field of technology, there is no single course that you can learn once and for all, no matter how good the course is, it can only be “the master leads the door, the practice depends on the individual”. “There is no end to learning”, in any field of technology, is not only a good habit, but also a necessary prerequisite for programmers and engineers to keep up with The Times and gain better opportunities and development.

If you feel that you are not learning efficiently and lack proper guidance, fineJoin the technical circle with rich resources and learning atmosphereLet’s learn and communicate!

Join us! We are committed to creating an equal and high-quality Android communication circle. It may not be able to make everyone’s technology advance rapidly in the short term, but in the long run, vision, pattern and long-term development direction are the most important.

The mid-life crisis at 35 is mostly due to being led by short-term interests and squeezing out the value too early. If we can establish a correct long-term career plan from the beginning. After 35, you’re only worth more than the people around you.