network

HTTP

The HTTP protocol (Hypertext Transfer Protocol) is a transport protocol used to transfer hypertext from WWW:World Wide Web servers to local browsers. HTTP is an application layer based on TCP/IP communication protocol to transfer data. Features of HTTP request-response model 1. Simple and fast: when a client requests service to a server, it only needs to transmit the request method and path. Request methods: commonly used GET, HEAD, POST. Because HTTP protocol is simple, the HTTP server program size is small, so the communication speed is very fast. 2. Flexibility: HTTP allows the transfer of data objects of any type. The Type being transferred is marked by content-Type. 3. Connectionless: Connectionless means that only one request can be processed per connection. The server disconnects from the customer after processing the request and receiving the reply from the customer. In this way, transmission time can be saved. Stateless: HTTP is a stateless protocol. Stateless means that the protocol has no memory for transaction processing. The lack of state means that if the previous information is needed for subsequent processing, it must be retransmitted, which can result in an increased amount of data being transmitted per connection. On the other hand, the server responds faster when it doesn’t need the previous information. 5, support B/S and C/S mode.

The differences between URIs and urls urls are as follows: Unified Resource Locator (URL) protocol (or service mode) 2 HOST IP address (sometimes including port number) 3 Host resource address. www.aspxfans.com:8080/news/index….

URI: Unified Resource Identifier (URI) 1 Naming mechanism for accessing resources 2 Host name for storing resources 3 Name of the resource itself, represented by a path

URN: Identifies the source with a name. Mailto :[email protected] URI = URL+URN. URI is the abstract URL and URN is the concrete

HTTP Request message Request

Request: line, requests the head, empty and request data line = HTTP version request type + + to access resources GET / 562 f25980001b1b106000338. HTTP / 1.1 JPG

Img.mukewang.com Accept image/webp,image/,/*; Q = 0.8

Blank lines: Blank lines after the request header are required

Request data body: the body of the post

Response: Status line, message header, blank line, and response body status line =HTTP protocol version + status code + Status message status code: 200 Succeeded 404 Requested resource does not exist… HTTP / 1.1 200 OK

Message header = Header additional information

Blank lines: Blank lines after the request header are required

Response body: HTML

1.GET data address bar, POST in body 2. Size of data transferred: The HTTP protocol does not limit the size of data transferred. But browsers have limitations. GET is about 2K, 2048 characters (URL limit), and POST is about 4M. 3. The security of POST is higher than that of GET. 4.GET is used to obtain or query resource information, and POST is used to update resource information

TCP/IP protocol

1.OSI seven-layer model



(1) Application layer: Applications access the network through this layer, such as FTP, HTTP, DNS, and TELNET

(2) Transport layer: TCP protocol and UDP protocol;

(3) Network layer: IP protocol, ARP, RARP protocol, ICMP

(4) Network interface layer: it is the basic layer of TCP/IP protocol and is responsible for sending and receiving data frames.

1.IP address 255.255.255.255 4 Groups 0-255 2. Domain name Indicates the alias of an IP address, which is resolved and translated by the DNS server. 3.MAC Address Physical address and hardware address

Encapsulation and distribution – ENCAPSULATION: When an application sends data, the data passes from top to bottom at each layer of the protocol hierarchy. Each layer adds header or tail information to the data. This information is called protocol data unit (PDU) distribution: When the host receives a data frame, the data rises from the bottom of the protocol layer. As it passes through each layer, the header or tail of the packet at the corresponding layer is checked and removed, which is the reverse of the encapsulation process.

TCP three-way handshake sequence number seQ: used to mark the sequence of data segments. Ack: The ack number refers to the number expected to receive the next byte. Request +1 acknowledgement ACK: The acknowledgement number field is valid only when ACK=1. ACK=0: Invalid acknowledgement number Synchronization SYN: Used to synchronize serial numbers when a connection is established. SYN=1 indicates that this is a connection request, and the handshake is complete with SYN=0. Terminate FIN: Used to release a connection.

Four times to wave

The first time: the client and the server are actively closed. FIN=1 (Finish) seQ request is generated to close the connection.

Second time: The server gets the request and says I got it. Reply ACK=1, seq=seq+1

Third time: the server initiates the CLOSE request FIN=1, the seQ value of the server, and then enters the CLOSE (closing state).

Fourth time: CLIENT reply I received ACK=1, seq=seq+1. And enter the TIME_WAITING state. This is the client is not closed, wait a maximum of 2*MSL (maximum message connection time, 2 minutes, generally 30 seconds) after closing. When the time was up, it CLOSED itself. After receiving the reply from the client, the server itself closes CLOSED.

Why is there a three-way handshake when you connect and a four-way handshake when you close? Because it is a full-duplex job (that is, the client and the server can send and receive requests to each other), the first time the client says I am not sending messages anymore. The server said I got it. The third time the server says, “I’m not sending any messages anymore.”

Why is time-wait state required? 1. Terminate the TCP connection reliably. Cannot ensure that the server received the final reply from the server (fourth wave). So just in case, wait for you, and if it doesn’t sound for a while, I’m turning it off. 2. The delayed TCP packet is discarded in sufficient time.

Why can’t we connect with two handshakes? Both parties should be prepared to send the data (both parties know that they are ready), but also allow both parties to negotiate the initial serial number, which is sent and confirmed during the handshake. Two handshakes tend to deadlock. The client does not receive a response from the server, one is waiting and the other thinks the connection has been established.

What if the connection has been established, but the client suddenly fails? TCP also has a keepalive timer, when the time reaches 2 hours, there is no response, send heartbeat packets, do not reply on the disconnect.

1. Perform DNS domain name resolution (local browser cache, operating system cache, or DNS server). http://www.baidu.com, search for IP address in local cache, search for IP address in operating system cache, search for IP address in DNS server) 2. 3) The client initiates an HTTP request. 4) The server responds to the HTTP request. 5) The client parses HTML code and requests resources in HTML code. Will change than connect. TCP connections will not be closed after HTTP1.0, after 1-6. Repeat steps 3-6. Http2.0 has IO multiplexing.)

The difference between TCP and UDP TCP provides a connection-oriented, reliable byte stream service with full duplex. UDP is a simple datagram – oriented transport layer protocol. It provides no reliability, just sending datagrams from the application to the IP layer, but with no guarantee that they will reach their destination. TCP is connection-oriented, UDP is connectionless TCP data header with serial number, acknowledgement number, and so on. In contrast, UDP program structure is simpler. TCP is byte stream oriented, UDP is based on datagram TCP guarantees data correctness, UDP may drop packets TCP guarantees data order, UDP does not guarantee UDP: live, game; TCP: Everything else

How many HTTP requests can be sent over a TCP connection to Http1.0, each time the connection is sent or received, the connection is disconnected. So there’s only one. After Http1.1, a connection is not closed after a TCP request, and multiple HTTP requests can be made. After Http2.0, there is multiplexing.

Java

HashMap

HashMap is a storage structure for key-value pairs <key,value> based on the Map interface, allowing null values while being non-ordered and non-synchronous (i.e., thread unsafe). The underlying implementation of HashMap is an array + a linked list + a red-black tree (red-black tree has been added to JDK1.8). It stores and finds data based on the hashCode value of the key. HashMap allows the key key of a record to be null at most. The common operations of HashMap, such as add, delete, change, search, etc., have good execution efficiency. It is a compromise implementation of data structures such as ArrayList and LinkedList.

The default size of the underlying hash table array is 16

float loadFactor; Load factor (default: 0.75) int threshold; Threshold for next capacity expansion (Threshold = container capacity capacity x Load factor). Node

[] table; The underlying array, which acts as a hash table, is always 2 to the NTH power.
,v>

What is the underlying implementation of HashMap? For example, store PUT (K key, V value), search for GET (Object key), delete Remove (Object key), and change replace(K key, V value).

put

Call the hash(key) method to calculate the hash value of the key

Table [I] (hash value % hash length)

get

Call the hash(key) method to calculate the hash value of the key

Remove Remove is the same as get. 1. Use the hash(key) method to calculate the hash value of the key. (Hash value % Hash table length) Calculate the storage location table[I]. 2. Check whether table[I] has a value. 4. Determine whether the linked list is red black tree, is red black tree delete 5. It’s not just going through a single list, finding it, and then deleting it

Replace, like GET, finds the node. GetNode () gets the value mapped to the key, records the old value of the element, assigns the new value to the element, returns the old value of the element, or returns null if no element is found.

Hash conflict If a new value is added, hashCode%length will result in a value that already has data in it. At this point, the new node is placed at the head of the original list and then inserted. This is called head insertion or head insertion. And when you insert something that already has a list in place, it’s called a hash collision or hash collision

Hash conflict resolution: chain address. A HashMap uses the hash address method to resolve hash collisions. When a conflicting element is inserted, it is inserted into the last digit of the list at that location, forming a single linked list. When a list of existing locations is 8 or larger, HashMap converts the list into a red-black tree to improve lookups.

Why does a HashMap have to be 2 to the NTH hash % length 1111&something else, where the numbers are more evenly distributed, with the same probability of each digit and even if it’s set to 7 it’s going to be the nearest 2 to the NTH number, 8

Load factor The load factor indicates the usage of the hash table space. The default is 0.75, when the TAB length is 16. 16*0.75=12, when the number of stored data is greater than 12, the hash collision will be serious, need to expand. 60-75% is best

HashMap and HashTable 1. The HashMap thread is not safe, but the HashTable thread is safe. 2. HashTable defaults to 11. Expand by 2+1

HashMap is not thread-safe, so how does it handle multithreading? Hashmaps are not thread-safe, and if multiple threads change data to the same HashMap at the same time, data inconsistencies or data contamination can result. If there is a thread unsafe operation, HashMap will throw ConcurrentModificationException prevent abnormal data as possible, when we are on a HashMap traversed, during traversal, we cannot add the HashMap, Delete the change data such as operation, otherwise will be throw ConcurrentModificationException, this is a fail – fast (quick) failure mechanism. If you want thread-safe, consider using ConcurrentHashMap. Multiple threads can also loop in an infinite loop during capacity expansion.

When we use a HashMap, what is the best object to choose as a key immutable object, that is, an object that the hashCode does not change. String, Integer, things like that.

HashSet

HashSet is implemented using HashMap.

Add calls the put() method of the HashMap directly, using the element itself as the key and PRESENT as the value, meaning that all values in the map are the same

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}
Copy the code

Remove Return map.remove(o)==PRESENT;

Contains checks whether the element exists

public boolean contains(Object o) {
   return map.containsKey(o);
}
Copy the code

Traverse elements

public Iterator<E> iterator() {
    return map.keySet().iterator();
}
Copy the code

(1) Inside the HashSet, the key of the HashMap is used to store elements, so as to ensure that elements are not repeated; (2) HashSet is unordered because the key of a HashMap is unordered; (3) A null element is allowed in a HashSet because a HashMap allows a null key; (4) HashSet is not thread-safe; (5) HashSet does not have a get() method;

JMM

becauseMemory interactionIO operations, read data from memory, etc., are not of the same order of magnitude as CPU speed, data from memory ->CPU, and CPU calculation. So we have to design a cache. We’re caching.

Cache consistency Cache-based storage system interactions work well to resolve the processor/memory speed conflict, but they also introduce greater complexity into computer systems because of a new problem: cache consistency. Multicore cpus each have their own caches and are prone to data inconsistencies with main RAM.

Out-of-order code execution optimization To improve computing efficiency, the CPU executes code out of order. In multi-core scenarios, one core may execute part of the logic, which may cause problems.

The JMM was designed to address the problem of smoothing out differences in memory access across hardware and operating systems, so that Java programs can achieve consistent memory access across platforms.

The main memory holds all the variables, and each thread has its own working memory, which holds copies of the variables.

But this leads to two problems, 1 working memory data consistency, thread safety issues and 2 instruction reordering optimization

JMM’s eight instructions: Variables acting on main memory: Lock Identifies thread state. A variable that has been unlocked can be locked by another thread. The read variable is transferred from main memory -> the thread’s working memory for subsequent load actions. Write variables are transferred from working memory -> main memory. After the store operation

Working memory variables: Load variables after read, stored in a copy of the variables in working memory. Use variable from working memory -> execution engine; This operation is performed when the virtual machine needs to use bytecode instructions. Assign execution engine -> Working memory. Store Working memory -> Main memory. Before you write.

Atomicity: A set of operations either all or none are performed. Visibility means that when multiple threads access the same variable and one thread changes the value of the variable, other threads can immediately see the changed value. Orderliness looks like code is executed sequentially

Memory barriers How do you ensure order and visibility of the underlying operations in Java? The memory barrier can be passed. A memory barrier is an instruction inserted between two CPU instructions to prevent processor instructions from being reordered (like a barrier). Volatile and synchronized use memory barriers. Volatile inserts memory barriers before and after each operation.

Final Once initialization is complete, the value of the final variable is written back to main memory.

Garbage Collection algorithm (JVM)

ART has a different mechanism from Dalvik. In Dalvik, every time an application is run, bytecode needs to be converted to machine code by just in time (JIT) compiler, which will slow down the application’s running efficiency. In ART environment, bytecode is precompiled into machine code when the application is installed for the first time, which greatly improves the running efficiency of the application and reduces the power consumption of the phone, making it a true native application. This process is called AOT (Ahead-of-time). In this way, application startup (first time) and execution will be faster. Advantages: System performance is significantly improved. Apps start up faster, run faster, have a smoother experience, and have more immediate tactile feedback. Longer battery life. Support for lower hardware.

Cons: Memory applications take longer to install.

Direct memory NIO introduces a Channel – and buffer-based I/O approach that allocates off-heap memory directly using Native libraries. Avoid time-consuming data operations back and forth between the Java heap and Native heap.

Object access locates handle access, direct pointer access

Determine whether reference counting and accessibility analysis should be recycled

GC Roots’ object virtual machine stack (the local variable table in the stack frame) references objects in the object method area referenced by class static attributes in the object method area referenced by constants in the object Native method stack reference objects

Finalize () can avoid death once if it is to be GC, Finalize (). The Finalize () method is called automatically by the system only once.

Garbage collection algorithm tag cleaning algorithm, copy algorithm, mark-collation algorithm

The happens-before principle. This principle is the primary basis for determining whether data is contested and threads are safe. Antecedent is a partial ordering relationship between two operations defined in the Java memory model.

Implementation of threads

Kernel threads = system-provided threads

Lightweight threads = commonly referred to as threads

Each lightweight process has a kernel level thread support.

Thread scheduling: Concurrent thread scheduling (thread execution time is controlled by the thread itself, not commonly used), preemptive thread scheduling

Vm class loading mechanism

The virtual machine loads the data describing the Class from the Class file into memory, verifies, resolves, and initializes the data, and eventually forms Java types that the virtual machine can use directly. In the Java language, type loading, concatenation, and initialization are all done during program execution.

The process of class loading

The life cycle consists of seven phases: load, validate, prepare, parse, initialize, use, and unload.

loading:

1. Obtain the binary byte stream by class name

2. Transform the static storage structure of the byte stream into the runtime data structure of the method area

3. Generate a Class object representing the Class in the heap as an access point to the data in the method area.

validation: validation of file format/metadata/bytecode/symbol references

To prepare: Allocates memory and sets initial values for class variables.

parsing: The process by which the virtual machine converts symbolic references in the constant pool to direct references.

Initialize theA static code block is used to specify an initial value for a class variable

(1) The JVM executes the main () method (2) class.forname () (3) classLoader.loadClass ()

Parent delegation Principle When a class loader receives a class loading task, it will first hand it to its parent class loader to complete it. Therefore, the final loading task will be passed to the top layer of the starting class loader. Only when the parent class loader fails to complete the loading task, it will attempt to execute the loading task.

Custom classloaders (1) adhere to the parent delegate model by inheriting the ClassLoader and overriding the findClass() method. (2) Break the parent delegate model: inherit the ClassLoader and override the loadClass() method. The first approach is generally recommended for customizing class loaders that adhere to the parent delegate model to the greatest extent possible.