2024-07-08
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
ArrayList is implemented based on arrays. It does this by dynamically expanding or reducing the size of the array. When the capacity is insufficient, it creates a larger array, copies the original data, and then adds the new data.
The mechanism of ArrayList expansion is: when the first element is added, the capacity of ArrayList is 10; each time a new element is added, if it exceeds the capacity, the original capacity will be doubled, that is, the original capacity*2. If the original capacity is 0, the new capacity will be 1.
The internal implementation of ArrayList is based on arrays. When multiple threads access the same ArrayList at the same time, data inconsistency may occur. For example, when one thread is reading the data of ArrayList and another thread is adding/deleting data to ArrayList, the data in ArrayList may change. In this way, the thread reading the ArrayList data may read incorrect data, causing program errors.
A stack is a special linear table, whose characteristic is that data can only be inserted and deleted at one end, following the principle of first-in, last-out and last-in, first-out. It is a storage structure that can be used to store function parameter values, local variables, etc.
A heap is a special tree structure, whose characteristic is that the values of all nodes are greater than or equal to the values of their child nodes, and the value of its root node is the largest or smallest. A heap is a dynamic storage structure that can be used to store large amounts of data, such as sorting and searching.
The essence of a coroutine is a lightweight thread. Each coroutine has a stack to store functions and their parameters, local variables, etc. The coroutine can be suspended, resumed, and switched, which is the basis for implementing coroutines.
State SynchronizationIt means that in each control cycle, the state (such as position, speed, acceleration, etc.) of each machine in the multi-machine system is transmitted to other machines so that each machine is synchronized. State synchronization can achieve real-time multi-machine collaborative control, but because a large amount of data needs to be transmitted in each control cycle, its accuracy may be relatively low.
Frame SynchronizationIt means that in each control cycle, the control commands of each machine in the multi-machine system are transmitted to other machines so that each machine is synchronized. Frame synchronization can achieve the accuracy of multi-machine collaborative control, but since only a small number of control commands are transmitted in each control cycle, its real-time performance may be relatively low.
HashMap is implemented in an array linked list (red-black tree) at the bottom. It stores data based on the hashCode value of the key. The position of the data in the array can be calculated based on the hash code (hash conflict), and the linked list (red-black tree) is used to store the conflicting data. In Java 8, when the linked list length exceeds the threshold (the default is 8), HashMap will be converted into a red-black tree to improve query efficiency. When the capacity is insufficient, it will automatically expand. The default load factor is 0.75, and the expansion method is twice the capacity.
What are the usage scenarios of stacks and queues?
The browser's forward and backward functions: The web pages visited by the browser can be realized through the stack data structure to achieve forward and backward functions.
The TCP packet sticking problem refers to the fact that the TCP protocol does not fragment data when transmitting data, resulting in the amount of data received by the receiving end being greater than the amount of data sent by the sending end.
First of all, UDP datagrams can help implement the three-way handshake process in the TCP/IP protocol. In the first handshake, a client sends a UDP datagram containing a handshake request. When the server receives this message, it will reply with a confirmation message, indicating that the server has received the client's handshake request and is ready to provide services. In the second handshake, the client will send a UDP datagram again. This time the message contains some useful information, such as the client's IP address, port number, etc., so that the server can identify the client. In the third handshake, the server will send a UDP datagram, indicating that the connection has been established and the client can start sending data.
Secondly, UDP datagrams can also help implement the data transmission process in the TCP/IP protocol. When the client needs to send data to the server, it will encapsulate the data into a UDP datagram and send it to the server; after receiving the UDP datagram, the server will parse the data contained in the message and perform relevant processing.
Finally, UDP datagrams can also help implement the end-of-connection process in the TCP/IP protocol. When the client no longer needs to communicate with the server, it can send a UDP datagram to indicate that the client wants to end the connection. After the server receives this message, it will release the corresponding resources, thus completing the entire TCP/IP protocol connection process.
Coroutines allow programs to switch between different tasks, thereby improving program efficiency and reducing program running time. Coroutines allow programs to switch between multiple tasks instead of waiting for one task to complete before starting another. It can also share variables between different threads, thereby reducing program running time. For multi-tasking applications, using coroutines can significantly improve performance, resulting in faster running speeds.
Arrays are faster because the address of each element in an array is continuous and fixed, so the address of the next element can be quickly obtained. However, the address of each element in a linked list is discontinuous, so it is necessary to traverse the pointer to obtain the address of the next element, so array traversal is faster.
A virtual function is a special function. The difference between it and a normal function is that it is automatically defined by the compiler and can be called at compile time. The characteristic of a virtual function is that its implementation is determined at runtime, not at compile time.
The main purpose of virtual functions is to achieve polymorphism. An abstract class can define multiple virtual functions, which are then implemented by its subclasses.
It does not have to be a virtual function, but it is generally recommended to use virtual functions because virtual functions can be overridden by derived classes, so that the destructor of the derived class can be executed correctly. If virtual functions are not used, the destructor of the derived class will not be called, which may cause memory leaks and other problems.
The rendering pipeline is a series of steps that transform the game scene data from input information to the image displayed on the screen.
The rendering pipeline process is divided into three main stages: preparation stage, geometry stage, and lighting stage.
In the preparation phase, the game engine loads the game scene's models and textures into the graphics processing unit (GPU) and organizes the data for use in subsequent stages.
During the geometry phase, matrix transformations are used to place the model in three-dimensional space and convert it into a form that can be supported by pixels on the screen.
In the lighting stage, the light source and lighting model are used to calculate the color value of each pixel, and the resulting image is finally displayed on the screen.
The conditions for the greedy algorithm to obtain the optimal solution are "optimal substructure" and "greedy selection property":