当前位置:网站首页>NiO related knowledge points (I)
NiO related knowledge points (I)
2022-07-07 05:00:00 【Certainly tomorrow】
Catalog
BIO、NIO difference
Conceptually
BIO: Blocking IO.
NIO: Non blocking IO.
Specifically reflect
BIO
BIO Server code . The specific blocking place is written in the code comment .
public static void main(String[] args) {
try {
ServerSocket socket = new ServerSocket(9000);
while (true){
System.out.println(" Waiting for the connection ");
Socket accept = socket.accept();// Block if connection is not obtained
System.out.println(" There is a client connection ");
handler(accept);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void handler(Socket accept) throws IOException {
byte[] bytes = new byte[1024];
System.out.println(" Ready to read ");
int read = accept.getInputStream().read(bytes); // Unread data blocks
System.out.println(" Finished reading ");
if (read!=-1){
System.out.println(" The data is :"+new String(bytes,0,read));
}
// The data has not been processed , Then block the next data
}
BIO It's too limited , The above code allows only one connection to be processed at a time . Let's make a little transformation , Even if we put handler Methods are placed in multiple threads , Can handle multiple messages . But there are still big problems : If too many requests cause memory overflow , Or there are multiple connections but empty connections that never send messages ( Waste of resources ).
BIO It has long been unable to meet the growing number of users . If your concurrency is small , It can still be used BIO Of .
NIO
NIO Server code . Essence is written in the notes .
static List<SocketChannel> list = new ArrayList<SocketChannel>();
public static void main(String[] args) throws IOException {
// Set port
ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(9000));
// Set the connection to non blocking
serverSocket.configureBlocking(false);
System.out.println(" Service started successfully ");
// Non blocking loop
while (true){
SocketChannel accept = serverSocket.accept();
// If there is a connection
if(accept!=null){
System.out.println(" There is a client connection ");
// Set the pipe to non blocking
accept.configureBlocking(false);
list.add(accept);
}
Iterator<SocketChannel> iterator = list.iterator();
// Traverse the pipeline to get data ,read It's not blocked .
while (iterator.hasNext()){
SocketChannel sc = iterator.next();
ByteBuffer buffer = ByteBuffer.allocate(128);
int read = sc.read(buffer);
if(read>0){
System.out.println(" Received a message :"+new String(buffer.array()));
}else if(read==-1){
iterator.remove();
System.out.println(" Client disconnected ");
}
}
}
}
Naturally, it can support multiple connections , You can set the connection and read Non blocking . One thread handles multiple clients . Then use traversal connection to receive data .
above NIO Problems with the program :while(iterator.hasNext()) Traversal efficiency is low .
To solve this problem , Introduced selector multiplexer . Separate connection events from read-write events , If there is a write event , Just traverse the written socket. The code is as follows , Explain in notes .
public static void main(String[] args) throws IOException {
// Open ports
ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(9000));
serverSocket.configureBlocking(false);
// open selector Handle channel.linux Helped us create a epoll example (epoll_create)
Selector selector = Selector.open();
// hold serverSocket Sign up to selector, And specify the listening connection
SelectionKey selectionKey = serverSocket.register(selector, SelectionKey.OP_ACCEPT);
System.out.println(" Service started successfully ");
while (true){
// Blocking waits for events to be processed to occur
selector.select(); // Bottom call epoll_ctl function , Enable listening . call epoll_wait Go see another
// Whether the collection has active events , If not, it will block .
// To get in selector All the events inside
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
// Traverse processing
while (iterator.hasNext()){
SelectionKey key = iterator.next();
// If the event represents a connection
if(key.isAcceptable()){
ServerSocketChannel server=(ServerSocketChannel)key.channel();
SocketChannel socketChannel = server.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector,SelectionKey.OP_READ);
System.out.println(" Client connection successful ");
}else if(key.isReadable()){ // If the event represents readable
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(128);
int len = socketChannel.read(byteBuffer);
if (len>0){
System.out.println(" receive messages :"+new String(byteBuffer.array()));
}else if(len==-1){
System.out.println(" disconnect ");
socketChannel.close();
}
}
iterator.remove();
}
}
}
This solves the problem of invalid traversal . to want to Learn more epoll, You can read this article
Java Medium socket (socket) And introduction to relevant concepts _ If you have bad luck, try to make it up -CSDN Blog Getting started with sockets , Those who don't understand can start learning https://blog.csdn.net/wai_58934/article/details/122848372?spm=1001.2014.3001.5501 That's all for today , White whoring live broadcast open class , Continue tomorrow
边栏推荐
- Can I specify a path in an attribute to map a property in my class to a child property in my JSON?
- In depth analysis of kubebuilder
- Using thread class and runnable interface to realize the difference between multithreading
- 一文搞懂常见的网络I/O模型
- Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
- Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
- 程序员上班摸鱼,这么玩才高端!
- [Yugong series] go teaching course 005 variables in July 2022
- Depth first traversal template principle of tree and graph
- Decorator basic learning 02
猜你喜欢
System framework of PureMVC
Decorator basic learning 02
How to design API interface and realize unified format return?
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
IMS data channel concept of 5g vonr+
A detailed explanation of head pose estimation [collect good articles]
A simple and beautiful regression table is produced in one line of code~
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
随机推荐
Analyse approfondie de kubebuilder
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
Meaning of 'n:m' and '1:n' in database design
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
Flex layout and usage
JS variable plus
System framework of PureMVC
leetcode 53. Maximum subarray maximum subarray sum (medium)
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
Jetson nano配置pytorch深度学习环境//待完善
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
acwing 843. N-queen problem
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
3.基金的类型
一文搞懂常见的网络I/O模型
Canteen user dish relationship system (C language course design)
A row of code r shows the table of Cox regression model
Why do many people misunderstand technical debt