Java interview questions - NIO related interview questions

Posted by rocketsprocket on Fri, 14 Jan 2022 08:59:49 +0100

1. Differences among NiO, BIO and AIO

BIO: the traditional network communication model is BIO, synchronous blocking IO

In fact, the server creates a ServerSocket, and then the client uses a Socket to connect to the ServerSocket of the server. When the ServerSocket receives a connection request, it creates a Socket and a thread to communicate with the Socket.

Disadvantages of this method: each time a client accesses, a thread needs to be created on the server to serve the client

In this way, when a large number of clients come, the number of threads on the server may reach thousands or even tens of thousands. In this way, the server may be overloaded and crash and die.

BIO model diagram:

Acceptor:

There are two classic design patterns of network services in the traditional IO model: one is multithreading, and the other is processing by thread pool.

If it is based on Multithreading mode, it is such a mode, which is also the Acceptor thread model.

NIO:

NIO is a synchronous non blocking IO, which is implemented based on Reactor model.

In fact, it is equivalent to a thread processing a large number of client requests, polling a large number of channels through a thread, obtaining a batch of channels with events each time, and then starting a thread processing for each request

The core here is non blocking. One thread of the selector can continuously poll the channel. All client requests will not be blocked and will come in directly. The big deal is to wait in line.

The core of optimizing BIO is that a client does not have data to interact all the time. There is no need to waste a thread. Therefore, the client chooses to let the thread rest. Only when the client has corresponding operations, it initiates notifications and creates a thread to process requests.

NIO: model diagram

Reactor model:

AIO

AIO: asynchronous non blocking IO, implemented based on Proactor model.

Each request sent by a connection will bind a Buffer, and then notify the operating system to complete asynchronous reading. At this time, you can do other things

After the operating system finishes reading, it will call your interface and give you the data read asynchronously by the operating system. At this time, you can get the data for processing and write back the data

In the process of writing back, the same is to give the operating system a Buffer, let the operating system finish writing, and notify you when it is finished.

Both processes have buffers, and data is read and written through buffers.

The main difference is that after the data is written to the buffer, it doesn't care about it, and the rest is left to the operating system.

The same is true for the data written back by the operating system. Write it to the Buffer, and notify the client to read the data after writing.

Summary:

1, Synchronous blocking I/O(BIO):

Synchronous blocking I/O, the server implementation mode is one thread per connection, that is, when the client has a connection request, the server needs to start a thread for processing. If the connection does not do anything, it will cause unnecessary thread overhead, which can be improved through the thread pool mechanism. BIO mode is applicable to the architecture with a small and fixed number of connections. This mode has high requirements for server resources, and concurrency is limited to applications. In jdk1 4 used to be the only io now, but the program is intuitive, simple and easy to understand

2, Synchronous non blocking I/O(NIO):

Synchronous non blocking I/O, the server implementation mode is one request and one thread, that is, the connection requests sent by the client will be registered on the multiplexer, and the multiplexer will start a thread for processing only when it polls that there are IO requests connected. NIO mode is applicable to architectures with a large number of connections and short connections (light operation), such as chat server. Concurrency is limited to applications and programming is complex. Jdk1 and 4 begin to support it

3, Asynchronous non blocking I/O(AIO):

Asynchronous non blocking I/O. the server implementation mode is one valid request and one thread. The IO requests of the client are completed by the operating system first, and then notify the server to start the thread for processing. AIO mode is applicable to architectures with a large number of connections and long connections (re operation), such as photo album server. It fully calls the OS to participate in concurrent operation. The programming is complex, jdk1 7 start support.

2 explain the three core concepts of NIO

 Buffer Buffer concept
 The cache is actually an array. All data is stored in the cache. Whether you want to write data or read data, you must first write the data to the buffer.

passageway Channel concept
 Channels can be compared to BIO In the stream, all data is stored in Buffer But it needs to pass through the channel Channel For transmission. adopt Channel We can take data from Buffer Read or write from Buffer. be careful: Channel Cannot directly access data by itself, Channel Only with Buffer Interact.

Buffer Responsible for storing data, Channel Responsible for transportation data.

selector Selector concept
Selector Selectors are used to monitor multiple at the same time channel Yes IO Events, utilization Selector Enables a single thread to manage multiple threads Channel. When Channel happen IO At the time of the event, selector Will handle events, and the thread is non blocking the rest of the time. Selector Yes no blocking IO The core of.

Buffer type
ByteBuffer
CharBuffer
ShortBuffer
IntBuffer
LongBuffer
FloatBuffer
DoubleBuffer

Type of channel
FileChannel,Corresponding file IO
DatagramChannel,corresponding UDP
SocketChannel,corresponding TCP of client
ServerSocketChannel,corresponding TCP of server

3 what are the disadvantages of bio? Why use NIO?

BIO Is synchronous blocking when a thread calls read() or write()The thread will be blocked until some data is read
,Or data is completely written. The thread can no longer do anything during this period. And a connection corresponds to a thread. When there are multiple threads
 When requesting read and write, many threads need to be started, which requires the maintenance of multiple threads, resulting in high resource consumption. Although it can solve multithreading with thread pool
 However, the efficiency is still very low.
NIO It is synchronous and non blocking, and one connection can process multiple requests. NIO Only when there are real read-write events connected,
The thread is non blocking at other times. And you don't have to create a thread for each connection, let alone maintain multiple lines
 The process avoids the context switching between multiple threads, resulting in the waste of resources.

4 how does NiO achieve synchronous non blocking?

One thread Thread Use a selector Selector Monitor multiple channels Channel Upper IO Event so that a thread can
 Can handle multiple IO event. Channel monitored by configuration Channel For non blocking, then when Channel Upper IO Before the event arrives,
The thread will select Method suspended, give way CPU resources. Until we hear Channel have IO The corresponding response will be made only when the event occurs
 And processing.
Selector It can detect whether there are on multiple registered channels IO Event occurrence(be careful:Multiple Channel You can register to by event
 Same Selector),If an event occurs, get the event, and then process each event accordingly. So you can use only one
 A single thread manages multiple channels, that is, multiple connections and requests.
Selector Only on the channel can there be real IO When the event occurs, it will be processed accordingly, so it is not necessary to create one for each connection
 Thread to avoid the waste of thread resources and the overhead caused by context switching between multiple threads.

5 BIO, NIO and AIO application scenarios

1,BIO This method is applicable to the architecture with a small and fixed number of connections. This method has high requirements for server resources, and concurrency is limited to
 In application, JDK1.4 The only choice before.
2,NIO This method is applicable to the architecture with a large number of connections and short connections (light operation), such as chat server, bullet screen system and server
 Inter office communication, etc. JDK1.4 Start support.
3AIO This method is used for architectures with a large number of connections and long connections (re operation), such as photo album server OS Participate in concurrent operations,
Programming is complicated, JDK7 Start support.

6. What are the corresponding Position, Mark, Capacity and Limit in the buffer?

capacity: The size of the buffer is the size of the data contained in it.
limit: yes buffer A limit on buffer use from this index You can't read data from the beginning.
position: Represents the data in the array that can be read and written index, Cannot be greater than limit. 
mark: It's something like a road sign, somewhere position When you're ready, set it up mark,A flag can now be set
 Subsequent call reset()Method can put position Reset to the one set at that time mark Come on. Go get position or limit adjustment
 Is less than mark This is discarded when the value of mark
 If using Direct Schema created Buffer If so, the intermediate buffer will be reduced and used directly DirectorBuffer Come on
 Storage of data.

7 Relationship between NiO components

1.Selector The selector corresponds to a thread,One thread corresponds to multiple threads Channel(connect)
2.every last Channel Each corresponds to one buffer
3.Buffer It's just a memory block,The bottom layer is an array
4.Which program to switch to Channel It is determined by events,Event Is an important concept
5.Selector According to different events,Switch on each channel.
6.Data is read and written through Buffer,adopt flip Method to realize bidirectional operation,This and BIO There are essentially different,
BIO Through one-way flow input Input stream or output The output stream is written directly to the channel
7.Channel It's also two-way

8. Summary of differences between select, poll and epoll

1,Supports the maximum number of connections that a process can open
select The maximum number of connections that can be opened by a single process is FD_SETSIZE Macro definition, whose size is the size of 32 integers (in 32-bit format)
On the machine, the size is 3232. Similarly, on the 64 bit machine FD_SETSIZE For 3264), of course, we can modify it and then reset it
 The kernel is newly compiled, but the performance may be affected, which requires further testing.
poll
poll Essentially and select There is no difference, but it has no limit on the maximum number of connections because it is stored based on a linked list
epoll
 Although there is an upper limit on the number of connections, it is very large, 1 G About 100000 connections can be opened on the memory machine, 2 G The memory of the machine can be turned on 20
 About 10000 connections
2,FD After the sharp increase IO Efficiency issues
select
 Because the connection will be traversed linearly every time it is called, as FD The increase of will cause the "linear degradation performance problem" of slow traversal speed.
poll
 ditto
epoll
 because epoll The implementation in the kernel is based on each fd Upper callback Function, only active socket Will actively call callback,So it's active socket In fewer cases, use epoll There is no linear degradation of the first two, but all socket When they are very active, there may be performance problems.
3, Message passing mode
select
 The kernel needs to transfer messages to the user space, and the kernel copy action is required
poll
 ditto
epoll
epoll It is realized by sharing a piece of memory between the kernel and user space.

9 NIO model, especially the responsibilities of the selector

 Here comes a new concept, selector,What kind of thing is it?
Think about a scene: in a chicken farm, there is such a person whose daily work is to constantly check several special chicken cages. If there are chickens in the cage
 Come on, if a chicken goes out, a chicken lays eggs, a chicken gets sick, etc., record the corresponding situation. If the person in charge of the chicken farm wants to know the situation,
Just ask the person.
Here, this person is quite Selector,Each chicken cage is equivalent to one SocketChannel,Each thread passes through a Selector can
 To manage multiple SocketChannel. 
In order to achieve Selector Manage multiple SocketChannel,The specific SocketChannel Object registered to Selector,Parallel sound
 Specify the events that need to be monitored (such as Selector To know what data needs to be recorded), there are four types of events:
1,connect: The client connects to the server. The corresponding value is SelectionKey.OP_CONNECT(8)
2,accept: The server receives the client connection event, and the corresponding value is SelectionKey.OP_ACCEPT(16)
3,read: Read event, the corresponding value is SelectionKey.OP_READ(1)
4,write: Write event. The corresponding value is SelectionKey.OP_WRITE(4)
This is well understood. Every time a request arrives at the server, it is from connect Start, connect After success, the server starts to prepare accept,
Ready, start reading data, process it, and finally write back the data return.
So, when SocketChannel When a corresponding event occurs, Selector Can be observed and treated accordingly.

Expanded NIO and Netty interview questions for top 10 factories

NIO
 elaborate NIO Principle?
BIO/NIO/AIO What's the difference? What are the implementations?
Tell NIO Principle and implementation of? NIO Which classical technical idea is used? JDK1.8 in NIO What optimization has been done
 Understand the common problems of multiplexing mechanism. What are the differences between synchronous blocking, synchronous non blocking and asynchronous?
select,poll,eopll What's the difference?
Linux network IO Model
 Which libraries or frameworks are used NIO?
redis The bottom implementation of event driven multiplexing; Extended to NIO programming
NIO What problems have been solved
 Yes mina?
NIO What is the core of? ( IO Line Cheng Chi), asked IO Package design pattern (decorator pattern), why do you design like this? Is there a better design?
NIO Model, especially the selector Responsibilities and implementation principles of
select,poll and epoll Differences between
NIO Process introduction, NIO How to multiplex
Netty
Netty How to do distributed task scheduling?
Netty What are the advantages of? Is there anything wrong with that?
NIO,Netty,Network protocol, involving OS interactive
Netty nio Questions, asked the process
Netty of API gate Design
Netty Thread model (source code torture)
Netty Several thread models and architectures of

We can discuss learning together. We can pay attention to, collect and forward. If there is any supplement, we can comment below

Topics: Java Interview NIO