java核心技術-NIO

1、reactor(反應器)模式

  使用單線程模擬多線程,提高資源利用率和程序的效率,增加系統吞吐量。下面例子比較形象的說明了什么是反應器模式:

  一個老板經營一個飯店,

  傳統模式 - 來一個客人安排一個服務員招呼,客人很滿意;(相當于一個連接一個線程)

  后來客人越來越多,需要的服務員越來越多,資源條件不足以再請更多的服務員了,傳統模式已經不能滿足需求。老板之所以為老板自然有過人之處,老板發現,服務員在為客人服務時,當客人點菜的時候,服務員基本處于等待狀態,(阻塞線程,不做事)。

  于是乎就讓服務員在客人點菜的時候,去為其他客人服務,當客人菜點好后再招呼服務員即可。 --反應器(reactor)模式誕生了

  飯店的生意紅紅火火,幾個服務員就足以支撐大量的客流量,老板用有限的資源賺了更多的money~~~~^_^

 通道:類似于流,但是可以異步讀寫數據(流只能同步讀寫),通道是雙向的,(流是單向的),通道的數據總是要先讀到一個buffer 或者 從一個buffer寫入,即通道與buffer進行數據交互。

  

通道類型:  

FileChannel:從文件中讀寫數據。  
DatagramChannel:能通過UDP讀寫網絡中的數據。  
SocketChannel:能通過TCP讀寫網絡中的數據。  
ServerSocketChannel:可以監聽新進來的TCP連接,像Web服務器那樣。對每一個新進來的連接都會創建一個SocketChannel。  
 - FileChannel比較特殊,它可以與通道進行數據交互, 不能切換到非阻塞模式,套接字通道可以切換到非阻塞模式;

緩沖區 - 本質上是一塊可以存儲數據的內存,被封裝成了buffer對象而已!

  

緩沖區類型:

ByteBuffer  
MappedByteBuffer  
CharBuffer  
DoubleBuffer  
FloatBuffer  
IntBuffer  
LongBuffer  
ShortBuffer  
常用方法:

allocate() - 分配一塊緩沖區  
put() - 向緩沖區寫數據
get() - 向緩沖區讀數據  
filp() - 將緩沖區從寫模式切換到讀模式  
clear() - 從讀模式切換到寫模式,不會清空數據,但后續寫數據會覆蓋原來的數據,即使有部分數據沒有讀,也會被遺忘;  
compact() - 從讀數據切換到寫模式,數據不會被清空,會將所有未讀的數據copy到緩沖區頭部,后續寫數據不會覆蓋,而是在這些數據之后寫數據
mark() - 對position做出標記,配合reset使用
reset() - 將position置為標記值    
緩沖區的一些屬性:

capacity - 緩沖區大小,無論是讀模式還是寫模式,此屬性值不會變;
position - 寫數據時,position表示當前寫的位置,每寫一個數據,會向下移動一個數據單元,初始為0;最大為capacity - 1,切換到讀模式時,position會被置為0,表示當前讀的位置

limit - 寫模式下,limit 相當于capacity 表示最多可以寫多少數據,切換到讀模式時,limit 等于原先的position,表示最多可以讀多少數據。

非直接緩沖區:通過allocate() 方法 分配緩沖區,將緩沖區建立在JVM內存中

直接緩沖區:通過allocateDirect() 方法直接緩沖區 將緩沖區建立在物理內存中

2.1 關于緩沖區各個屬性的測試

    String str = "abcde";//1. 分配一個指定大小的緩沖區ByteBuffer buf = ByteBuffer.allocate(1024);System.out.println("--------------allocate()----------------");System.out.println(buf.position());//0System.out.println(buf.limit());//1024System.out.println(buf.capacity());//1024//2. 利用put存入數據到緩沖區中去buf.put(str.getBytes());System.out.println("----------------put()-------------------");System.out.println(buf.position());//5System.out.println(buf.limit());//1024System.out.println(buf.capacity());//1024//3. 切換到讀取模式buf.flip();System.out.println("----------------flip()------------------");System.out.println(buf.position());//0System.out.println(buf.limit());//5System.out.println(buf.capacity());//1024//4. 利用get() 讀取緩沖區中的數據byte[] dst = new byte[buf.limit()];buf.get(dst);System.out.println(new String(dst,0,dst.length));System.out.println("----------------get()------------------");System.out.println(buf.position());//5System.out.println(buf.limit());//5System.out.println(buf.capacity());//1024//5.可重復讀buf.rewind();System.out.println("----------------rewind()------------------");System.out.println(buf.position());//0System.out.println(buf.limit());//5System.out.println(buf.capacity());//1024//6.clear(): 清空緩沖區, 但是緩沖區的數據依然存在, 但是處于被遺忘的狀態buf.clear();System.out.println("----------------clear()-------------------");System.out.println(buf.position());//0System.out.println(buf.limit());//1024System.out.println(buf.capacity());//1024byte[] newByte = new byte[buf.limit()];buf.get(newByte);System.out.println(new String(newByte,0,newByte.length));

2.2 關于通道的使用

1.利用通道進行 文件的復制 非直接緩沖區

    FileInputStream fis = null;FileOutputStream fos = null;FileChannel inChannel = null;FileChannel outChannel = null;try {fis = new FileInputStream("1.jpg");fos = new FileOutputStream("2.jpg");// ①獲取通道inChannel = fis.getChannel();outChannel = fos.getChannel();// ②將通道中的數據存入緩沖區ByteBuffer byteBuffer = ByteBuffer.allocate(1024);// 將通道中的數據存入緩沖區while (inChannel.read(byteBuffer) != -1) {byteBuffer.flip(); // 切換讀取數據的模式outChannel.write(byteBuffer);byteBuffer.clear();}} catch (IOException e) {e.printStackTrace();} finally {if (inChannel != null) {try {inChannel.close();} catch (IOException e) {e.printStackTrace();}}if (outChannel != null) {try {outChannel.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}

2.通道之間的傳輸

CREATE_NEW:如果文件不存在就創建,存在就報錯

CREATE:如果文件不存在就創建,存在創建(覆蓋)

    FileChannel inChannel = null;FileChannel outChannel = null;try {inChannel = FileChannel.open(Paths.get("hello.txt"), StandardOpenOption.READ);outChannel = FileChannel.open(Paths.get("hello2.txt"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);inChannel.transferTo(0, inChannel.size(), outChannel);} catch (Exception e) {e.printStackTrace();}  finally {if(inChannel != null){try {inChannel.close();} catch (IOException e) {e.printStackTrace();}}if(outChannel != null){try {outChannel.close();} catch (IOException e) {e.printStackTrace();}}}
  1. 使用直接緩沖區完成內存文件的復制

    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);outChannel = FileChannel.open(Paths.get("x.jpg"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);MappedByteBuffer inMappedBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());MappedByteBuffer outMappedBuffer = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());System.out.println(inMappedBuffer.limit());byte[] b = new byte[inMappedBuffer.limit()];;inMappedBuffer.get(b);outMappedBuffer.put(b);} catch (Exception e) {e.printStackTrace();
    } finally {if(inChannel != null){try {inChannel.close();} catch (IOException e) {e.printStackTrace();}}if(outChannel != null){try {outChannel.close();} catch (IOException e) {e.printStackTrace();}}}

    2.3 重點 NIO-非阻塞IO

個人認為 NIO 最難的兩點 一個是對于選擇器和選擇鍵的理解 其次是對于網絡通信模型的理解

本章內容以防過長 只講解 NIO 的使用方法 上述兩點參看下回分解

阻塞IO示例:

//客戶端
@Test
public void client() throws IOException{SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898));FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);ByteBuffer buf = ByteBuffer.allocate(1024);while(inChannel.read(buf) != -1){buf.flip();sChannel.write(buf);buf.clear();}sChannel.shutdownOutput();//接收服務端的反饋int len = 0;while((len = sChannel.read(buf)) != -1){buf.flip();System.out.println(new String(buf.array(), 0, len));buf.clear();}inChannel.close();sChannel.close();
}//服務端
@Test
public void server() throws IOException{ServerSocketChannel ssChannel = ServerSocketChannel.open();FileChannel outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);ssChannel.bind(new InetSocketAddress(9898));SocketChannel sChannel = ssChannel.accept();ByteBuffer buf = ByteBuffer.allocate(1024);while(sChannel.read(buf) != -1){buf.flip();outChannel.write(buf);buf.clear();}//發送反饋給客戶端buf.put("服務端接收數據成功".getBytes());buf.flip();sChannel.write(buf);sChannel.close();outChannel.close();ssChannel.close();
}

非阻塞IO示例-TCP:

//客戶端br/>@Test
public void client() throws IOException{
//1. 獲取通道
SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898));

    //2. 切換非阻塞模式sChannel.configureBlocking(false);//3. 分配指定大小的緩沖區ByteBuffer buf = ByteBuffer.allocate(1024);//4. 發送數據給服務端Scanner scan = new Scanner(System.in);while(scan.hasNext()){String str = scan.next();buf.put((new Date().toString() + "\n" + str).getBytes());buf.flip();sChannel.write(buf);buf.clear();}//5. 關閉通道sChannel.close();
}//服務端
@Test
public void server() throws IOException{//1. 獲取通道ServerSocketChannel ssChannel = ServerSocketChannel.open();//2. 切換非阻塞模式ssChannel.configureBlocking(false);//3. 綁定連接ssChannel.bind(new InetSocketAddress(9898));//4. 獲取選擇器Selector selector = Selector.open();//5. 將通道注冊到選擇器上, 并且指定“監聽接收事件”ssChannel.register(selector, SelectionKey.OP_ACCEPT);//6. 輪詢式的獲取選擇器上已經“準備就緒”的事件while(selector.select() > 0){//7. 獲取當前選擇器中所有注冊的“選擇鍵(已就緒的監聽事件)”Iterator<SelectionKey> it = selector.selectedKeys().iterator();while(it.hasNext()){//8. 獲取準備“就緒”的是事件SelectionKey sk = it.next();//9. 判斷具體是什么事件準備就緒if(sk.isAcceptable()){//10. 若“接收就緒”,獲取客戶端連接SocketChannel sChannel = ssChannel.accept();//11. 切換非阻塞模式sChannel.configureBlocking(false);//12. 將該通道注冊到選擇器上sChannel.register(selector, SelectionKey.OP_READ);}else if(sk.isReadable()){//13. 獲取當前選擇器上“讀就緒”狀態的通道SocketChannel sChannel = (SocketChannel) sk.channel();//14. 讀取數據ByteBuffer buf = ByteBuffer.allocate(1024);int len = 0;while((len = sChannel.read(buf)) > 0 ){buf.flip();System.out.println(new String(buf.array(), 0, len));buf.clear();}}//15. 取消選擇鍵 SelectionKeyit.remove();}}
}

非阻塞IO示例-UDP:

@Test
public void send() throws IOException{DatagramChannel dc = DatagramChannel.open();dc.configureBlocking(false);ByteBuffer buf = ByteBuffer.allocate(1024);Scanner scan = new Scanner(System.in);while(scan.hasNext()){String str = scan.next();buf.put((new Date().toString() + ":\n" + str).getBytes());buf.flip();dc.send(buf, new InetSocketAddress("127.0.0.1", 9898));buf.clear();}dc.close();
}@Test
public void receive() throws IOException{DatagramChannel dc = DatagramChannel.open();dc.configureBlocking(false);dc.bind(new InetSocketAddress(9898));Selector selector = Selector.open();dc.register(selector, SelectionKey.OP_READ);while(selector.select() > 0){Iterator<SelectionKey> it = selector.selectedKeys().iterator();while(it.hasNext()){SelectionKey sk = it.next();if(sk.isReadable()){ByteBuffer buf = ByteBuffer.allocate(1024);dc.receive(buf);buf.flip();System.out.println(new String(buf.array(), 0, buf.limit()));buf.clear();}}it.rem    歡迎工作一到五年的Java工程師朋友們加入Java群: 891219277

群內提供免費的Java架構學習資料(里面有高可用、高并發、高性能及分布式、Jvm性能調優、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用自己每一分每一秒的時間來學習提升自己,不要再用"沒有時間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個交代!

轉載于:https://blog.51cto.com/14084556/2324364

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389048.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389048.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389048.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

物種分布模型_減少物種分布建模中的空間自相關

物種分布模型Species distribution models (SDM; for review and definition see, e.g., Peterson et al., 2011) are a dominant paradigm to quantify the relationship between environmental dynamics and several manifestations of species biogeography. These statisti…

BZOJ1014: [JSOI2008]火星人prefix

BZOJ1014: [JSOI2008]火星人prefix Description 火星人最近研究了一種操作&#xff1a;求一個字串兩個后綴的公共前綴。 比方說&#xff0c;有這樣一個字符串&#xff1a;madamimadam&#xff0c;我們將這個字符串的各個字符予以標號&#xff1a; 序號&#xff1a; 1 2 3 4 5 6…

redis將散裂中某個值自增_這些Redis命令你都掌握了沒?

本章主要內容字符串命令、列表命令和集合命令散列命令和有序集合命令發布命令與訂閱命令其他命令本章將介紹一些沒有在第1章和第2章出現過的Redis命令&#xff0c;學習這些命令有助于讀者在已有示例的基礎上構建更為復雜的程序&#xff0c;并學會如何更好地去解決自己遇到的問題…

asp.net的MessageBox

public class MessageBox{ public enum MsgButton { /// <summary> /// 只是OK按鈕 /// </summary> OK 1, /// <summary> /// 提示是否確定 /// </summary> OKCancel 2 } publ…

深入理解激活函數

為什么需要非線性激活函數&#xff1f; 說起神經網絡肯定會降到神經函數&#xff0c;看了很多資料&#xff0c;也許你對激活函數這個名詞會感覺很困惑&#xff0c; 它為什么叫激活函數&#xff1f;它有什么作用呢&#xff1f; 看了很多書籍上的講解說會讓神經網絡變成很豐富的…

如何一鍵部署項目、代碼自動更新

為什么80%的碼農都做不了架構師&#xff1f;>>> 摘要&#xff1a;my-deploy:由nodejs寫的一個自動更新工具,理論支持所有語言(php、java、c#)的項目,支持所有git倉庫(bitbucket、github等)。github效果如何?如果你的后端項目放在github、bitbucket等git倉庫中管理…

Kettle7.1在window啟動報錯

實驗環境&#xff1a; window10 x64 kettle7.1 pdi-ce-7.1.0.0-12.zip 錯誤現象&#xff1a; a java exception has occurred 問題解決&#xff1a; 運行調試工具 data-integration\SpoonDebug.bat //調試錯誤的&#xff0c;根據錯誤明確知道為何啟動不了&#xff0c;Y--Y-…

opa847方波放大電路_電子管放大電路當中陰極電阻的作用和選擇

膽機制作知識視頻&#xff1a;6P14單端膽機用示波器方波測試輸出波形詳細步驟演示完整版自制膽機試聽視頻&#xff1a;膽機播放《猛士的士高》經典舞曲 熟悉的旋律震撼的效果首先看下面這一張300B電子管電路圖&#xff1a;300B單端膽機原理圖圖紙里面畫圓圈的電阻就是放大電路當…

鍵盤鉤子

C#鍵盤鉤子//*************************鍵盤鉤子********************** //定義變量 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); static int hKeyboardHook 0; HookProc KeyboardHookProcedure; /************************* * 聲明API函數 * ***…

matplotlib基礎函數函數 plot, figure

matplotlib.pyplot.plot(*args, scalexTrue, scaleyTrue,dataNone,**kwargs) 用線段和標記去繪制x和y。調用簽名&#xff1a; plot([x], y, [fmt], *, dataNone, **kwargs) plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)點或線的坐標由x, y給出 操作參數 fmt 是為了…

清潔數據ploy n_清潔屋數據

清潔數據ploy nAs a bootcamp project, I was asked to analyze data about the sale prices of houses in King County, Washington, in 2014 and 2015. The dataset is well known to students of data science because it lends itself to linear regression modeling. You …

redis安裝redis集群

NoSql數據庫之Redis1、什么是nosql&#xff0c;nosql的應用場景2、Nonsql數據庫的類型a) Key-valueb) 文檔型&#xff08;類似于json&#xff09;c) 列式存儲d) 圖式3、redis的相關概念kv型的。4、Redis的安裝及部署5、Redis的使用方法及數據類型a) Redis啟動及關閉b) Redis的數…

聯想拯救者y7000p加內存條_內存、硬盤不夠用?手把手教你升級聯想拯救者Y7000P...

由于這兩年內存價格的高企&#xff0c;主流筆記本的內存容量被鎖定在 8GB 已經有了相當長的時間。作為近幾個月最熱門的游戲本產品&#xff0c;聯想拯救者 Y7000P 除頂配之外同樣使用的是 8GB 內存和 512GB 固態硬盤的配置。所以買到這款機器的玩家多數都會選擇進行內存和硬盤的…

機器學習實踐一 logistic regression regularize

Logistic regression 數據內容&#xff1a; 兩個參數 x1 x2 y值 0 或 1 Potting def read_file(file):data pd.read_csv(file, names[exam1, exam2, admitted])data np.array(data)return datadef plot_data(X, y):plt.figure(figsize(6, 4), dpi150)X1 X[y 1, :]X2 X[…

ajax+webservice

版本為AJAX November CTP 三個示例分別為&#xff1a;1 帶參數的WS方法2 不帶參數的WS方法3 參數類型為DataTable的WS方法 一、WebMethod注意要點&#xff1a;1 WebMethod類需要添加命名空間 Microsoft.Web.Script.Services&#xff0c;此空間需要引用Microsoft.Web.Preview.dl…

深度學習數據擴張_適用于少量數據的深度學習結構

作者&#xff1a;Gorkem Polat編譯&#xff1a;ronghuaiyang導讀一些最常用的few shot learning的方案介紹及對比。傳統的CNNs (AlexNet, VGG, GoogLeNet, ResNet, DenseNet…)在數據集中每個類樣本數量較多的情況下表現良好。不幸的是&#xff0c;當你擁有一個小數據集時&…

時間管理

時間管理 時間管理是運用策略和技術&#xff0c;幫助你盡可能有效地利用你的時間。 不僅僅是要將時間用在正確的地方&#xff0c; 而且還要將盡可能有效地加以利用。 目前是如何利用時間的&#xff1a; 意識是時間管理的先決條件。目標提供路線圖。選擇是難點。 意識 第一條…

基于邊緣計算的實時績效_基于績效的營銷中的三大錯誤

基于邊緣計算的實時績效We’ve gone through 20% of the 21st century. It’s safe to say digitalization isn’t a new concept anymore. Things are fully or at least mostly online, and they tend to escalate in the digital direction. That’s why it’s important to…

本線程鉤子

鉤子其實就是調用一下API而已&#xff1a; 1、安裝鉤子&#xff1a;   SetWindowsHookEx 函數原形&#xff1a;HHOOK SetWindowsHookEx( int idHook, // 鉤子類型&#xff0c; HOOKPROC lpfn, // 鉤子函數地址…

Maven Web項目解決跨域問題

跨域問題目前筆者所用到的方案大致有三種:jsonp,SpringMVC 4以上注解方式和cros三方過濾器。 Jsonp JSONP(JSON with Padding)是一個非官方的協議&#xff0c;它允許在服務器端集成Script tags返回至客戶端&#xff0c;通過javascript callback的形式實現跨域訪問&#xff08;這…