java进阶-Netty
作者:小教学发布时间:2023-09-28分类:程序开发学习浏览:79
导读:Netty在此非常感谢尚硅谷学院以及韩顺平老师在B站公开课Netty视频教程Nettydemo代码文件I/O说NIO之前先说一下BIO(Blocki...
Netty
在此非常感谢尚硅谷学院以及韩顺平老师在B站公开课
Netty视频教程
Netty demo代码文件
I/O
说NIO之前先说一下BIO(Blocking IO),如何理解这个Blocking呢?客户端监听(Listen)时,Accept是阻塞的,只有新连接来了,Accept才会返回,主线程才能继读写socket时,Read是阻塞的,只有请求消息来了,Read才能返回,子线程才能继续处理读写socket时,Write是阻塞的,只有客户端把消息收了,Write才能返回,子线程才能继续读取下一个请求
BIO
- 适用连接数目少且固定的服务
package com.example.demo01.netty;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BIODemo01 {
public static void main(String[] args) throws IOException {
ExecutorService executorService = Executors.newCachedThreadPool();
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("服务器启动成功。。。");
while(true){
try {
System.out.println(Thread.currentThread().getName() + " 等待连接正在阻塞。。。。。。");
final Socket socket = serverSocket.accept();
executorService.submit(() -> {
socketHandler(socket);
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static void socketHandler(Socket socket) {
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
while (true){
System.out.println(Thread.currentThread().getName() + " 读取数据正在阻塞。。。");
int read = inputStream.read(bytes);
if(read != -1){
System.out.println(new String(bytes, 0, read));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
NIO
- 适用连接数目多且连接比较短的服务
- jdk1.4开始支持
AIO
- 适用连接数目多且连接比较长的服务
- jdk7开始支持
NIO原理
FIleBuffer
package com.example.demo01.netty.nio;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NioFileChannel {
public static void main(String[] args) throws Exception {
String str = new String("测试文档");
ByteBuffer allocate = ByteBuffer.allocate(1024);
allocate.put(str.getBytes());
allocate.flip();
FileOutputStream fileOutputStream = new FileOutputStream("d:\\test.txt");
FileChannel channel = fileOutputStream.getChannel();
channel.write(allocate);
fileOutputStream.close();
}
}
package com.example.demo01.netty.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class NioFileChannelInputStream {
public static void main(String[] args) throws IOException {
File file = new File("d:\\test.txt");
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel channel = fileInputStream.getChannel();
ByteBuffer allocate = ByteBuffer.allocate((int) file.length());
channel.read(allocate);
System.out.println(new String(allocate.array()));
fileInputStream.close();
}
}
package com.example.demo01.netty.nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class NioTrancfer {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("d:\\1.jpeg");
FileOutputStream fileOutputStream = new FileOutputStream("d:\\2.jpeg");
FileChannel sourceChannel = fileInputStream.getChannel();
FileChannel destChannel = fileOutputStream.getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
destChannel.close();
fileInputStream.close();
fileOutputStream.close();
}
}
selector
selectionKey
零拷贝
Reactor
单Reactor单线程
单Reactor多线程
主从Reactor多线程
Netty线程模型
- 简单版
- 详细版
- 程序开发学习排行
-
- 1鸿蒙HarmonyOS:Web组件网页白屏检测
- 2HTTPS协议是安全传输,为啥还要再加密?
- 3HarmonyOS鸿蒙应用开发——数据持久化Preferences
- 4记解决MaterialButton背景颜色与设置值不同
- 5鸿蒙HarmonyOS实战-ArkUI组件(RelativeContainer)
- 6鸿蒙HarmonyOS实战-ArkUI组件(Stack)
- 7鸿蒙HarmonyOS实战-ArkUI组件(GridRow/GridCol)
- 8[Android][NDK][Cmake]一文搞懂Android项目中的Cmake
- 9鸿蒙HarmonyOS实战-ArkUI组件(mediaquery)
- 最近发表
-
- WooCommerce最好的WordPress常用插件下载博客插件模块的相关产品
- 羊驼机器人最好的WordPress常用插件下载博客插件模块
- IP信息记录器最好的WordPress常用插件下载博客插件模块
- Linkly for WooCommerce最好的WordPress常用插件下载博客插件模块
- 元素聚合器Forms最好的WordPress常用插件下载博客插件模块
- Promaker Chat 最好的WordPress通用插件下载 博客插件模块
- 自动更新发布日期最好的WordPress常用插件下载博客插件模块
- WordPress官方最好的获取回复WordPress常用插件下载博客插件模块
- Img to rss最好的wordpress常用插件下载博客插件模块
- WPMozo为Elementor最好的WordPress常用插件下载博客插件模块添加精简版