日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

netty:IO模型

發(fā)布時間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 netty:IO模型 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

BIO,NIO模型

BIO代碼實現(xiàn)

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 BIOServer {public static void main(String[] args) throws IOException{ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();ServerSocket serverSocket = new ServerSocket(8888);System.out.println("服務(wù)器啟動...");while(true){// 監(jiān)聽final Socket socket = serverSocket.accept();System.out.println("連接一個客戶端...");newCachedThreadPool.execute(new Runnable(){@Overridepublic void run() {handler(socket);}});}}public static void handler(Socket socket){try {byte[] bytes = new byte[1024];InputStream ins = socket.getInputStream();while(true){int readLength = ins.read(bytes);if(-1 != readLength){System.out.println("線程: " + Thread.currentThread().getName() + " ");System.out.println(new String(bytes,0,readLength));}else{break;}}} catch (Exception e) {e.printStackTrace();}finally{try {socket.close();} catch (IOException e) {e.printStackTrace();}}} }

使用telnet連接ServerSocket

telnet 127.0.0.1 8888

NIO(non-blocking IO)同步非阻塞

NIO有三大核心部分, Channel(通道), Buffer(緩沖區(qū)), Selector(選擇器)
1.每個Channel都會對應(yīng)一個Buffer。
2.Selector對應(yīng)一個線程,一個線程對應(yīng)多個channel。
3.程序切換到哪個channel是由事件決定的,Event(事件)就是一個重要的概念。
4.Buffer是一個內(nèi)存塊,底層是有一個數(shù)組。
5.數(shù)據(jù)的讀取寫入是通過Buffer,這個和BIO不一樣, BIO中是通過輸入流,輸出流兩個流進行讀取,寫入的。而NIO僅僅只通過Buffer就完成讀取寫入。
6.Channel也是雙向的,可以返回底層操作系統(tǒng)的情況,比如linux底層的操作系統(tǒng)通道就是雙向的。

buffer

Channel(通道)

  • 通道可以同時進行讀寫,但流只能讀或者寫
  • 通道可以實現(xiàn)異步讀寫數(shù)據(jù)
  • 通道可以從緩沖(buffer)讀數(shù)據(jù),也可以寫數(shù)據(jù)到緩沖(buffer)
    FileChannel用于文件的數(shù)據(jù)讀寫。
    DatagramChannel用于UDP的數(shù)據(jù)讀寫。
    ServerSocketChannel和SocketChannel用于TCP的數(shù)據(jù)讀寫。
  • 向本地文件寫數(shù)據(jù)

    import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel;public class NIOFileChannel01 {public static void main(String[] args) throws Exception{String str = "hello, 風云爭霸ddd";// 創(chuàng)建一個輸出流FileOutputStream fos = new FileOutputStream("C:\\Users\\haha\\Desktop\\2.txt");// 通過FileOutputStream 獲取對應(yīng)的 FileChannel// 這個fileChannel的真是類型是FileChannelImplFileChannel fileChannel = fos.getChannel();// 創(chuàng)建一個緩沖區(qū) ByteBufferByteBuffer bf = ByteBuffer.allocate(1024);// 將str放入bf中bf.put(str.getBytes());bf.flip();// 將bf數(shù)據(jù)寫入到fileChannelfileChannel.write(bf);fos.close();} }

    從本地文件讀數(shù)據(jù)

    import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel;public class NIOFileChannel02 {public static void main(String[] args) throws Exception{ // 創(chuàng)建一個輸出流FileInputStream fis = new FileInputStream("C:\\Users\\haha\\Desktop\\2.txt");// 通過FileOutputStream 獲取對應(yīng)的 FileChannel// 這個fileChannel的真是類型是FileChannelImplFileChannel fileChannel = fis.getChannel();// 創(chuàng)建一個緩沖區(qū) ByteBufferByteBuffer bf = ByteBuffer.allocate(1024);// 將str放入bf中fileChannel.read(bf);System.out.println(new String(bf.array()));fis.close();} }

    Buffer類相關(guān)方法

    ByteBuffer類(最常用的Buffer類)

    NIO還支持通過多個Buffer(即Buffer數(shù)組)完成讀寫操作, 即Scattering 和 Gathering

    import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel;/** Scattering: 將數(shù)據(jù)寫入到buffer時,可以采用buffer數(shù)組,依次寫入* Gathering: 從buffer讀取數(shù)據(jù)時,可以采用buffer數(shù)組,依次讀*/ public class ScatteringAndGatheringTest {public static void main(String[] args) throws Exception{// 使用ServerSocketChannel,ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();InetSocketAddress inetSocketAddress = new InetSocketAddress(7000);// 綁定端口到socket,并啟動serverSocketChannel.socket().bind(inetSocketAddress);// 創(chuàng)建buffer數(shù)組ByteBuffer[] byteBuffers = new ByteBuffer[2];byteBuffers[0] = ByteBuffer.allocate(5);byteBuffers[1] = ByteBuffer.allocate(3);// 等待客戶端連接SocketChannel socketChannel = serverSocketChannel.accept();int messageLength = 8;// 循環(huán)的讀取while(true){int byteRead = 0;while(byteRead < messageLength){long l = socketChannel.read(byteBuffers);byteRead += l; // 累計讀取的字節(jié)數(shù)System.out.println("byteRead=" + byteRead);// 看看當前buffer的position,limitfor(int i=0; i<byteBuffers.length; i++){System.out.println("position="+byteBuffers[i].position()+" "+"limit="+byteBuffers[i].limit());}}// 將所有buffer進行flipfor(int i=0; i<byteBuffers.length; i++){byteBuffers[i].flip();}// 將數(shù)據(jù)顯示到客戶端long byteWrite = 0;while(byteWrite < messageLength){long l = socketChannel.write(byteBuffers);byteWrite += l;}// 將所有buffer進行clearfor(int i=0; i<byteBuffers.length; i++){byteBuffers[i].clear();}System.out.println("byteRead="+byteRead+" byteWrite="+byteWrite);}} } 《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

    以上是生活随笔為你收集整理的netty:IO模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 欧美热热 | 一级片黄色 | 亚洲国产成人精品女人久久 | 91麻豆成人| 一级黄色毛毛片 | 天天综合入口 | 超碰在线人人草 | 国产精品一区二区欧美 | 三级黄色网 | 污污免费在线观看 | 成人有色视频 | 精品乱码一区二区三区四区 | 在线不卡一区 | 91精品91久久久中77777老牛 | 5566毛片| 91九色网站 | 男人天堂资源 | 专干老肥女人88av | 亚洲国产精品一区二区久久hs | 99久久综合网 | 日韩一级 | 日本美女三级 | 欧美黑吊大战白妞欧美大片 | 少妇无套内谢久久久久 | 国产精品精 | 精品成人国产 | xxxx日本少妇 | 日本中文字幕有码 | 少妇伦子伦精品无吗 | 国产女人毛片 | 天天摸天天 | 欧美日韩一区二区在线视频 | 亚洲国产无码精品 | 亚洲色图久久 | 日韩午夜高清 | 精品欧美久久久 | 日韩少妇裸体做爰视频 | 久爱精品| 亚洲一区网站 | 日韩av自拍偷拍 | 中文字幕乱码一二三区 | 国模无码视频一区二区三区 | 夜夜天天干 | 久久国产视频播放 | 黄视频国产 | www.波多野结衣.com | 亚洲天堂2021av | 久久综合色视频 | 国产日韩一区 | 4438x五月天| 亚洲精品成a人在线观看 | 777黄色| 国产夫妻在线 | 色狠狠操| 二区三区在线观看 | 免费观看av网址 | 亚洲婷婷免费 | 97超碰资源站 | 久久久久久久久久久影视 | 97国产精品视频 | 毛片视频软件 | 久久久精品人妻一区二区三区四 | 日韩大片免费看 | 国产欧美一区二区三区另类精品 | 中文字幕35页 | 岛国精品在线播放 | 性欧美丰满熟妇xxxx性 | 麻豆视频网站在线观看 | 亚洲av无码精品色午夜果冻不卡 | 亚洲乱码中文字幕久久孕妇黑人 | 亚洲精品综合久久 | 久久精品天天中文字幕人妻 | 靠逼在线观看 | 九九综合九九综合 | 国产视频首页 | 99精品区 | 一级特黄性色生活片 | 综合久久亚洲 | 午夜激情在线观看视频 | 亚洲国产毛片aaaaa无费看 | www.99re7.com | 久久精品97 | 黄色片的网站 | 日韩欧美一区二区三区在线 | 亚洲福利国产 | 91精品国产福利在线观看 | 国产人与zoxxxx另类 | 超污视频网站 | 国产精品一卡二卡在线观看 | 欧美xxxx黑人又粗又长密月 | 国家队动漫免费观看在线观看晨光 | 国产精品高潮呻吟 | 国产精品熟妇人妻g奶一区 a少妇 | 欧美精品一区二区不卡 | 在线观看国产亚洲 | 国产麻豆一区 | 成年人免费在线看 | 国模大尺度自拍 | jizjiz中国少妇高潮水多 |