1. Summary of basic interview questions in the Java series

1. Whether the constructor can be overridden

First of all, the constructor cannot be inherited, because the class name of each class is different, and the constructor name is the same as the class name, so there is no inheritance at all.

And since constructors cannot be inherited, they cannot be overridden. However, in the same class, the constructor can be overloaded

2. Can the String class be inherited? Why?

No, because the String class is final, and the class modified by final cannot be inherited

3. The difference between String, StringBuffer, and StringBuilder

  • String: Immutable, each operation on String will generate a new object, which is inefficient and wastes memory space
  • StringBuffer: variable character sequence, low efficiency, thread safe
  • StringBuilder: variable character sequence, high efficiency, thread unsafe

StringBuilder and StringBuffer inherit AbstractStringBuilder. The default initial capacity of no parameters is 16.
When adding a string whose length exceeds 16, the capacity will be expanded: double its own length and add 2. If it still cannot fit, expand the capacity to the required length minCapacity

private int newCapacity(int minCapacity) {
    
    
        int newCapacity = (value.length << 1) + 2;
        if (newCapacity - minCapacity < 0) {
    
    
            newCapacity = minCapacity;
        }
}       

4. Do you understand the thread pool quintuple?

The thread pool quintuple refers to the five basic information that each thread needs to maintain when using the thread pool, including the process ID (PID) to which the thread belongs, the thread ID (TID), and the port number (PORT) that the thread listens to. ), thread status and task queue and other information.

Among them, PID is a process ID, which is used to uniquely identify a process, and is a unique identifier assigned to a process by the operating system; TID is a thread ID, which is used to uniquely identify a thread, and is a unique identifier assigned to a thread by the operating system; PORT is a port number, which is used to identify a network connection. Different threads can listen to the same port number and be distinguished by different TIDs; the thread status is the current running status of the thread, including idle, busy, waiting, etc.; the task queue is It is a queue in the thread pool used to store tasks to be executed.

Maintaining this information allows the thread pool to manage threads more efficiently, improve the reuse rate of threads, and avoid the overhead of frequently creating and destroying threads, thereby improving the performance of the program

5. What are the IO models? Tell me about the difference between nio, bio, and aio that you understand. Talk about the reactor model.

In computer network programming, commonly used IO models mainly include blocking IO (Blocking IO, BIO), non-blocking IO (Non-Blocking IO, NIO), multiplexing IO (Multiplexing IO, MIO) and asynchronous IO (Asynchronous IO , AIO).

  • Blocking IO (BIO): The thread will block and wait until the IO operation is completed, and other tasks cannot be processed during this period. This model is suitable for scenarios with low concurrency and slow processing of concurrent requests.
  • Non-blocking IO (NIO): The thread does not block waiting for the IO operation to complete, but polls the IO status to determine whether it is ready, and if it is not ready, it can process other tasks. But this method needs to poll all the time, which will increase the burden on the CPU.
  • Multiplexed IO (MIO): Process multiple IO requests through one thread. Typical implementation methods include select, poll, epoll, etc. This method can effectively reduce the burden on the CPU, but at the same time there will be performance bottlenecks.
  • Asynchronous IO (AIO): After the thread initiates the IO operation, it can immediately return to continue performing other tasks, and then call back to notify the thread after the IO operation is completed. This method is suitable for scenarios where there are many concurrent requests and the concurrent processing speed is fast.

In Java, the commonly used IO models are BIO, NIO and AIO, among which:

  • BIO: Java standard IO model, using blocking IO mode, suitable for scenarios with low concurrency and slow processing of concurrent requests.
  • NIO: The Java New IO model, which uses the multiplexed IO method, is suitable for scenarios with a large number of concurrent requests and a fast request processing speed. Compared with BIO, NIO can better handle high concurrent requests.
  • AIO: Java Asynchronous IO model, using asynchronous IO mode, suitable for scenarios that need to process a large number of IO requests. Compared with NIO, AIO is more efficient and can better utilize CPU resources.

Reactor mode is an event-driven IO model that uses a separate thread to receive and distribute events, rather than assigning a thread to each connection. When a new connection arrives, the Reactor thread will add it to an event queue, and then process the events in the queue through one or more IO threads. This model can effectively reduce the number of threads and improve the concurrency and scalability of the system. In Java, the Selector used by NIO is an implementation of the Reactor pattern

6. Introduce the commonly used Map

  • TreeMap: based on red-black tree implementation, implements SortedMap interface, sortable
  • HashMap: based on hash table implementation
  • HashTable: Similar to HashMap, but it is thread-safe. It is a legacy class and should not be used instead use ConcurrentHashMap to support thread safety
  • LinkedHashMap: Use a doubly linked list to maintain the order of elements, the order is insertion order or least recently used (LRU) order

This article is continuously updated

Guess you like

Origin blog.csdn.net/SJshenjian/article/details/130173785