Netty-NIO線程模型
點擊上方藍色字體,選擇“標星公眾號”
優(yōu)質(zhì)文章,第一時間送達
IO模型基本說明
BIO:
線程模型圖
代碼實例
/**
* @Author qrn
* @Date 2021/5/9 下午9:20
* @Version 1.0
* @blog https://blog.csdn.net/qq_41971087
* 實現(xiàn)一個簡單的http服務(wù):
*/
public class HttpServiceTest {
public static void main(String[] args) throws IOException{
//創(chuàng)建一個 ServerSocket 綁定8801端口
ServerSocket serverSocket = new ServerSocket(8801);
while (true){
try{
Socket socket = serverSocket.accept();
service(socket);
}catch (IOException e){
e.printStackTrace();
}
}
}
//模擬HTTP報文,輸出hell,nio到瀏覽器
private static void service(Socket socket){
try{
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
printWriter.println("HTTP/1.1 200 OK");
printWriter.println("Content-Type:text/html;charset=utf-8");
String body = "hello,nio";
printWriter.println("Content-Length:" + body.getBytes().length);
printWriter.println();
printWriter.write(body);
printWriter.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
服務(wù)器通信原理
業(yè)務(wù)場景:
NIO:
線程模型圖
核心組件:
Selector:
Channe:
Buffer:
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
代碼實例:
/**
* @Author qrn
* @Date 2021/5/5 下午9:43
* @Version 1.0
* @blog https://blog.csdn.net/qq_41971087
*/
public class BasicBuffer {
public static void main(String[] args) {
/**
* 創(chuàng)建一個buff 大小為5
*/
IntBuffer allocate = IntBuffer.allocate(5);
for(int i=0;i<allocate.capacity();i++){
allocate.put(i*2);
}
//讀寫轉(zhuǎn)換
allocate.flip();
//循環(huán)打印buff的值
while (allocate.hasRemaining()){
System.out.println(allocate.get());
}
}
}
業(yè)務(wù)場景:
AIO:
描述
業(yè)務(wù)場景:
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/qq_41971087/article/details/116572886
粉絲福利:Java從入門到入土學習路線圖
??????

??長按上方微信二維碼 2 秒
感謝點贊支持下哈 
評論
圖片
表情



