日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java基礎(chǔ)之NIO介紹及使用

瀏覽:133日期:2022-08-13 13:34:00
一、NIO

java.nio全稱(chēng)java non-blocking IO,是指jdk1.4 及以上版本里提供的新api(New IO) ,為所有的原始類(lèi)型(boolean類(lèi)型除外)提供緩存支持的數(shù)據(jù)容器,使用它可以提供非阻塞式的高伸縮性網(wǎng)絡(luò)。

二、三大組件

NIO三大組件:Channel、Buffer、Selector

1.Channel 和Buffer

Channel是一個(gè)對(duì)象,可以通過(guò)它讀取和寫(xiě)入數(shù)據(jù)。拿 NIO 與原來(lái)的 I/O 做個(gè)比較,通道就像是流,而且他們面向緩沖區(qū)(Buffer)的。所有數(shù)據(jù)都通過(guò)Buffer對(duì)象來(lái)處理,永遠(yuǎn)不會(huì)將字節(jié)直接寫(xiě)入通道中,而是將數(shù)據(jù)寫(xiě)入包含一個(gè)或多個(gè)字節(jié)的緩沖區(qū)。也不會(huì)直接從通道中讀取字節(jié),而是將數(shù)據(jù)從通道讀入緩沖區(qū),再?gòu)木彌_區(qū)獲取這個(gè)字節(jié)。

Channel是讀寫(xiě)數(shù)據(jù)的雙向通道,可以從Channel將數(shù)據(jù)讀取Buffer,也可將Buffer的數(shù)據(jù)寫(xiě)入Channel,而之前的Stream要么是輸入(InputStream)、要么是輸出(OutputStream),只在一個(gè)方向上流通。 而通道(Channel)可以用于讀、寫(xiě)或者同時(shí)用于讀寫(xiě)

常見(jiàn)的Channel

1.FileChannel

2.DatagramChannel

3.SocketChannel

4.ServerSocketChannel

Buffer緩沖區(qū)用來(lái)讀寫(xiě)數(shù)據(jù),常見(jiàn)的Buffer

1.ByteBuffer

2.ShortBuffer

3.IntBuffer

4.LongBuffer

5.FloatBuffer

6.DoubleBuffer

7.CharBuffer

2.Selector

​ 在多線程模式下,阻塞IO時(shí),一個(gè)線程只能處理一個(gè)請(qǐng)求,比如http請(qǐng)求,當(dāng)請(qǐng)求響應(yīng)式關(guān)閉連接,釋放線程資源。Selector選擇器的作用就是配合一個(gè)線程來(lái)管理多個(gè)Channel,獲取這些Channel上發(fā)生的事件,這些Channel工作在非阻塞模式下,不會(huì)讓線程一直在一個(gè)Channel上,適合連接數(shù)特別多,但流量低的場(chǎng)景。

java基礎(chǔ)之NIO介紹及使用

調(diào)用Selector的select()方法會(huì)阻塞直到Channel發(fā)送了讀寫(xiě)就緒事件,這些事件發(fā)生,select()方法就會(huì)

返回這些事件交給thread來(lái)處理。

三、ByteBuffer的使用

屬性

Buffer的讀寫(xiě)操作就是通過(guò)改變position,mark,limit的值來(lái)實(shí)現(xiàn)。注意他們之間的關(guān)系可以很輕松的讀、寫(xiě)、覆蓋。

position:對(duì)于寫(xiě)入模式,表示當(dāng)前可寫(xiě)入數(shù)據(jù)的下標(biāo),對(duì)于讀取模式,表示接下來(lái)可以讀取的數(shù)據(jù)的下標(biāo)。當(dāng)前操作位置的下標(biāo)。position()方法獲取。 mark:用來(lái)標(biāo)志當(dāng)前操作位置,調(diào)用mark()方法,使mark = position,可以在讀和寫(xiě)切換過(guò)程中標(biāo)記前一個(gè)操作下標(biāo)位置。 limit:Buffer的限界值。對(duì)于寫(xiě)模式,相當(dāng)于可寫(xiě)入的最大值,數(shù)組長(zhǎng)度。對(duì)于讀模式,表示最多可以讀取的數(shù)據(jù)的位置下標(biāo),通過(guò)flip()方法進(jìn)行讀寫(xiě)切換,原理改變position,mark,limit的值。 capacity:數(shù)組容量大小

方法

Buffer的方法全是根據(jù)改變position的值進(jìn)行操作的。

put():put方法寫(xiě)入數(shù)據(jù),可以單個(gè)byte字節(jié),或者byte數(shù)組。或者其它類(lèi)型,根據(jù)Buffer實(shí)例而定。 get():get方法讀取數(shù)據(jù),可以傳入byte數(shù)組和不傳參讀取一個(gè)字節(jié)。 mark():標(biāo)記當(dāng)前下標(biāo)position位置,mark = position 。讀寫(xiě)操作切換或者特殊要求時(shí),標(biāo)記當(dāng)前的下標(biāo)位置。 reset():將position 值重置為mark()方法標(biāo)記的值。 array():Buffer內(nèi)數(shù)據(jù)的byte數(shù)組。沒(méi)有值的位用0補(bǔ)位。 flip():limit為position值,將position置為0,mark初始值,寫(xiě)入操作切換為讀操作。 rewind():將position 和 mark設(shè)為初始值,調(diào)用這個(gè)可以一直讀取內(nèi)容或者一直寫(xiě)入覆蓋之前內(nèi)容,從第一位開(kāi)始讀/寫(xiě)。 clear():將屬性值還原。之前array()數(shù)組的值還在。 hasRemaining():判斷是否到最后四、測(cè)試Demo

private static void buffer1() { String data = 'abc'; //byte[] bytes = new byte[1024]; //創(chuàng)建一個(gè)字節(jié)緩沖區(qū),1024byte ByteBuffer byteBuffer = ByteBuffer.allocate(15); System.out.println(byteBuffer.toString()); // 標(biāo)記當(dāng)前下標(biāo)position位置,mark = position ,返回當(dāng)前對(duì)象 System.out.println(byteBuffer.mark()); //對(duì)于寫(xiě)入模式,表示當(dāng)前可以寫(xiě)入的數(shù)組大小,默認(rèn)為數(shù)組的最大長(zhǎng)度,對(duì)于讀取模式,表示當(dāng)前最多可以讀取的數(shù)據(jù)的位置下標(biāo) System.out.println(byteBuffer.limit()); // 對(duì)于寫(xiě)入模式,表示當(dāng)前可寫(xiě)入數(shù)據(jù)的下標(biāo),對(duì)于讀取模式,表示接下來(lái)可以讀取的數(shù)據(jù)的下標(biāo) System.out.println(byteBuffer.position()); //capacity 表示當(dāng)前數(shù)組的容量大小 System.out.println(byteBuffer.capacity()); //將字節(jié)數(shù)據(jù)寫(xiě)入 byteBuffer byteBuffer.put(data.getBytes()); //保存了當(dāng)前寫(xiě)入的數(shù)據(jù) for(Byte b : byteBuffer.array()){System.out.print(b + ' '); } System.out.println(); System.out.println(new String(byteBuffer.array())); //讀寫(xiě)模式切換 改變 limit,position ,mark的值 byteBuffer.flip(); //切換為寫(xiě)模式,將第一個(gè)字節(jié)覆蓋 byteBuffer.put('n'.getBytes()); // abc 改為 nbc System.out.println(new String(byteBuffer.array())); //讓position為之前標(biāo)記的值,調(diào)用mark()方法是將mark = position,這里將position = mark,mark為初始值拋出異常 byteBuffer.mark(); byteBuffer.reset(); //將position 和 mark設(shè)為初始值,調(diào)用這個(gè)可以一直讀取內(nèi)容或者一直寫(xiě)入覆蓋之前內(nèi)容,從第一位開(kāi)始讀/寫(xiě) byteBuffer.rewind(); for(Byte b : byteBuffer.array()){System.out.print(b + ' '); } System.out.println(); System.out.println(byteBuffer.toString()); //找到可寫(xiě)入的開(kāi)始位置,不覆蓋之前的數(shù)據(jù) byteBuffer.compact(); System.out.println(byteBuffer.toString());}

寫(xiě)入讀取完整操作

private static void buffer(){ //寫(xiě)入的數(shù)據(jù) String data = '1234'; //創(chuàng)建一個(gè)字節(jié)緩沖區(qū),1024byte ByteBuffer byteBuffer = ByteBuffer.allocate(15); //寫(xiě)入數(shù)據(jù) byteBuffer.put(data.getBytes()); //打輸出 49 50 51 52 0 0 0 0 0 0 0 0 0 0 0 println(byteBuffer.array()); byteBuffer.put((byte) 5); //追加寫(xiě)入 輸出: 49 50 51 52 5 0 0 0 0 0 0 0 0 0 0 println(byteBuffer.array()); //覆蓋寫(xiě)入 byteBuffer.flip(); //將寫(xiě)入下標(biāo)的 position = 0 byteBuffer.put((byte) 1); byteBuffer.put((byte) 2); byteBuffer.put((byte) 3); byteBuffer.put((byte) 4); byteBuffer.put((byte) 5); // 打印輸出 : 1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 println(byteBuffer.array()); //讀取數(shù)據(jù)操作 byteBuffer.flip();//從頭開(kāi)始讀 while (byteBuffer.position() != byteBuffer.limit()){System.out.println(byteBuffer.get()); } //此時(shí) position 和 數(shù)據(jù)數(shù) limit相等 System.out.println(byteBuffer.toString()); //批量讀取 byteBuffer.flip(); //將 position 置位0,從頭開(kāi)始操作 //創(chuàng)建一個(gè)byte數(shù)組,數(shù)組大小為可讀取的大小 byte[] bytes = new byte[byteBuffer.limit()]; byteBuffer.get(bytes); //輸出bytes 1 2 3 4 5 println(bytes);} private static void println(byte[] bytes){ for(Byte b : bytes){ System.out.print(b + ' '); } System.out.println();}

字符串跟ByteBuffer之間的轉(zhuǎn)換

public static void main(String[] args) { /*======================字符串轉(zhuǎn)buffer===========================*/ //1.字符串 轉(zhuǎn)為buytebuffer 需要轉(zhuǎn)為讀模式再進(jìn)行讀取操作 ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put('nio'.getBytes()); //2.charset 自動(dòng)轉(zhuǎn)為讀模式 ByteBuffer buffer1 = StandardCharsets.UTF_8.encode('nio'); //3, warp 自動(dòng)轉(zhuǎn)為讀模式 ByteBuffer buffer2 = ByteBuffer.wrap('nio'.getBytes()); /*======================buffer轉(zhuǎn)字符串===========================*/ String str = StandardCharsets.UTF_8.decode(buffer1).toString(); System.out.println(str);}五、Channel的使用

文件編程FileChannel

FileChannel只能工作在阻塞模式下,不能配合在Selector使用,Channel是雙向通道,但是FileChannel根據(jù)獲取源頭判定可讀或可寫(xiě)

FileInputStream獲取,只可讀 FileOutputStream獲取,只可寫(xiě) RandomAccessFile獲取,可讀、可寫(xiě)(雙向通道)

** * 文件流對(duì)象打開(kāi)Channel,F(xiàn)ileChannel是阻塞的 * @throws Exception */private static void channel() throws Exception{ FileInputStream in = new FileInputStream('C:UserszqqDesktop123.txt'); FileOutputStream out = new FileOutputStream('C:UserszqqDesktop321.txt'); //通過(guò)文件輸入流創(chuàng)建通道channel FileChannel channel = in.getChannel(); //獲取FileChannel FileChannel outChannel = out.getChannel(); //創(chuàng)建緩沖區(qū)byteBuffer ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //將管道channel中數(shù)據(jù)讀取緩存區(qū)byteBuffer中,channel只能與Buffer交互 while (channel.read(byteBuffer) != -1){//position設(shè)置為0,從頭開(kāi)始讀byteBuffer.flip();outChannel.write(byteBuffer);//byteBuffer 屬性還原byteBuffer.clear(); } //關(guān)閉各種資源 channel.close(); out.flush(); out.close(); in.close(); out.close();}

/** * 靜態(tài)方法打開(kāi)Channel * @throws Exception */public static void channel1() throws Exception{ // StandardOpenOption.READ :讀取一個(gè)已存在的文件,如果不存在或被占用拋出異常 // StandardOpenOption.WRITE :以追加到文件頭部的方式,寫(xiě)入一個(gè)已存在的文件,如果不存在或被占用拋出異常 // StandardOpenOption.APPEND:以追加到文件尾部的方式,寫(xiě)入一個(gè)已存在的文件,如果不存在或被占用拋出異常 // StandardOpenOption.CREATE:創(chuàng)建一個(gè)空文件,如果文件存在則不創(chuàng)建 // StandardOpenOption.CREATE_NEW:創(chuàng)建一個(gè)空文件,如果文件存在則報(bào)錯(cuò) // StandardOpenOption.DELETE_ON_CLOSE:管道關(guān)閉時(shí)刪除文件 //創(chuàng)建讀通道 FileChannel inChannel = FileChannel.open(Paths.get('C:UserszqqDesktop123.txt'), StandardOpenOption.READ); FileChannel outChannel = FileChannel.open(Paths.get('C:UserszqqDesktop321.txt'), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE); //內(nèi)存映射 MappedByteBuffer inByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY,0,inChannel.size()); MappedByteBuffer outByteBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE,0,inChannel.size()); //直接對(duì)緩沖區(qū)數(shù)據(jù)讀寫(xiě) byte[] bytes = new byte[inByteBuffer.limit()]; inByteBuffer.get(bytes);//讀取inByteBuffer內(nèi)的數(shù)據(jù),讀到bytes數(shù)組中 outByteBuffer.put(bytes);//寫(xiě)入到outByteBuffer inChannel.close(); outChannel.close();}

RandomAccessFile打開(kāi)通道Channel

/** * 通過(guò)RandomAccessFile獲取雙向通道 * @throws Exception */private static void random() throws Exception{ try (RandomAccessFile randomAccessFile = new RandomAccessFile('D:workspaceDemotest.txt','rw')){//獲取ChannelFileChannel fileChannel = randomAccessFile.getChannel();//截取字節(jié)//fileChannel.truncate(10);//創(chuàng)建ByteBuffer,注意文件大小ByteBuffer byteBuffer = ByteBuffer.allocate(1024);fileChannel.read(byteBuffer);System.out.println(new String(byteBuffer.array(),'GBK'));byteBuffer.clear();String data = 'this is datar';byteBuffer.put(data.getBytes());byteBuffer.flip();//讀寫(xiě)切換while (byteBuffer.hasRemaining()){ fileChannel.write(byteBuffer);}//將通道數(shù)據(jù)強(qiáng)制寫(xiě)到磁盤(pán)fileChannel.force(true); }}

FileChannel數(shù)據(jù)傳輸transferTo

/** * 一個(gè)文件向另一個(gè)文件傳輸(copy) */private static void transferTo() { try ( FileChannel inChannel = new FileInputStream('C:Users44141Desktopdemoin.txt').getChannel(); FileChannel outChannel = new FileOutputStream('C:Users44141Desktopdemoout.txt').getChannel() ){//底層使用操作系統(tǒng)零拷貝進(jìn)行優(yōu)化,效率高。限制2ginChannel.transferTo(0,inChannel.size(),outChannel); }catch (Exception e){e.printStackTrace(); }}/** * 大于2g數(shù)據(jù) */private static void transferToGt2g() { try ( FileChannel inChannel = new FileInputStream('C:Users44141Desktopdemoin.txt').getChannel(); FileChannel outChannel = new FileOutputStream('C:Users44141Desktopdemoout1.txt').getChannel() ){//記錄inChannel初始化大小long size = inChannel.size();//多次傳輸for(long rsize = size; rsize > 0;){ //transferTo返回位移的字節(jié)數(shù) rsize -= inChannel.transferTo((size-rsize),rsize,outChannel);} }catch (Exception e){e.printStackTrace(); }}六、網(wǎng)絡(luò)編程

阻塞模式

阻塞模式服務(wù)端每個(gè)方法都會(huì)阻塞等待客戶端操作。比如accept()方法阻塞等待客戶端連接,read()方法阻塞等待客戶端傳送數(shù)據(jù),并發(fā)訪問(wèn)下沒(méi)法高效的進(jìn)行工作。

1.服務(wù)端代碼

private static void server() throws Exception{ //1.創(chuàng)建服務(wù)器 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //2.綁定監(jiān)聽(tīng)端口 serverSocketChannel.bind(new InetSocketAddress(8080)); while (true){System.out.println('開(kāi)始監(jiān)聽(tīng)連接=============' );//4.accept 監(jiān)聽(tīng)進(jìn)來(lái)的連接 返回SocketChannel對(duì)象,accept默認(rèn)阻塞SocketChannel socketChannel = serverSocketChannel.accept();System.out.println('有連接連入===============' );ByteBuffer byteBuffer = ByteBuffer.allocate(1024);// read()是阻塞方法,等客戶端發(fā)送數(shù)據(jù)才會(huì)執(zhí)行socketChannel.read(byteBuffer);byteBuffer.flip();String str = StandardCharsets.UTF_8.decode(byteBuffer).toString();System.out.println('接收到數(shù)據(jù)=============:' + str); }}

2.客戶端代碼

private static void client() throws Exception { //1.創(chuàng)建客戶端 SocketChannel socketChannel = SocketChannel.open(); //連接服務(wù)端 socketChannel.connect(new InetSocketAddress('localhost',8080)); //2 秒后寫(xiě)入數(shù)據(jù) Thread.sleep(2 * 1000); socketChannel.write(StandardCharsets.UTF_8.encode('nio')); System.out.println();}

非阻塞模式

服務(wù)端設(shè)置成非阻塞模式。客戶端不用動(dòng)。

private static void server() throws Exception{ ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //設(shè)置成非阻塞模式 serverSocketChannel.configureBlocking(false); serverSocketChannel.bind(new InetSocketAddress(8080)); while (true){SocketChannel socketChannel = serverSocketChannel.accept();//非阻塞模式下,SocketChannel會(huì)返回為nullif(socketChannel != null){ //非阻塞模式 socketChannel.configureBlocking(false); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); socketChannel.read(byteBuffer); byteBuffer.flip(); String str = StandardCharsets.UTF_8.decode(byteBuffer).toString(); System.out.println('接收到數(shù)據(jù)=============:' + str);} }}七、Selector

Selector選擇器的作用就是配合一個(gè)線程來(lái)管理多個(gè)Channel,被Selector管理的Channel必須是非阻塞模式下的,所以Selector沒(méi)法與FileChannel(FileChannel只有阻塞模式)一起使用。

創(chuàng)建Selector

創(chuàng)建Selector只需要調(diào)用一個(gè)靜態(tài)方法

//創(chuàng)建SelectorSelector selector = Selector.open();

Selector可以用來(lái)監(jiān)聽(tīng)Channel的事件,總共有四個(gè)事件:

accept:會(huì)在有連接請(qǐng)求時(shí)觸發(fā),靜態(tài)常量 SelectionKey.OP_ACCEPT connect:客戶端建立連接后觸發(fā),靜態(tài)常量 SelectionKey.OP_CONNECT read:可讀時(shí)觸發(fā),靜態(tài)常量 SelectionKey.OP_READ write:可寫(xiě)時(shí)觸發(fā),靜態(tài)常量 SelectionKey.OP_WRITE

Selector與Channel綁定

//設(shè)置成非阻塞模式serverSocketChannel.configureBlocking(false);SelectionKey selectionKey = serverSocketChannel.register(selector,0,null);//綁定關(guān)注的事件selectionKey.interestOps(SelectionKey.OP_ACCEPT);八、網(wǎng)絡(luò)編程(多路復(fù)用)

1.客戶端代碼 SocketChannel

public static void main(String[] args) throws Exception { client();}private static void client() throws Exception { //1.創(chuàng)建客戶端 SocketChannel socketChannel = SocketChannel.open(); //連接服務(wù)端 socketChannel.connect(new InetSocketAddress('localhost',8080)); //2 秒后寫(xiě)入數(shù)據(jù) Thread.sleep(2 * 1000); socketChannel.write(StandardCharsets.UTF_8.encode('nio')); //3.讀取服務(wù)端返回?cái)?shù)據(jù) ByteBuffer byteBuffer = ByteBuffer.allocate(1024); socketChannel.read(byteBuffer); byteBuffer.flip(); System.out.println('服務(wù)端返回?cái)?shù)據(jù)=======:' + StandardCharsets.UTF_8.decode(byteBuffer).toString()); //斷開(kāi)連接 socketChannel.close();}

2.服務(wù)端代碼

public static void main(String[] args) throws Exception{ server();}private static void server() throws Exception{ //創(chuàng)建Selector Selector selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //設(shè)置成非阻塞模式 serverSocketChannel.configureBlocking(false); //將Channel注冊(cè)到selector上(綁定關(guān)系) //當(dāng)事件發(fā)生后可以根據(jù)SelectionKey知道哪個(gè)事件和哪個(gè)Channel的事件 SelectionKey selectionKey = serverSocketChannel.register(selector,0,null); //selectionKey 關(guān)注ACCEPT事件(也可以在上面注冊(cè)時(shí)用第二個(gè)參數(shù)設(shè)置) selectionKey.interestOps(SelectionKey.OP_ACCEPT); serverSocketChannel.bind(new InetSocketAddress(8080)); while (true){System.out.println('阻塞====================');// select方法沒(méi)有事件發(fā)生時(shí)阻塞線程,有事件線程會(huì)恢復(fù)運(yùn)行selector.select();System.out.println('開(kāi)始處理事件=================');// 處理事件Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while (iterator.hasNext()){ SelectionKey sk = iterator.next(); //獲取到SelectionKey對(duì)象之后,將集合內(nèi)的引用刪掉(Selecotr不會(huì)自動(dòng)刪除) iterator.remove(); //取消事件,不操作(不處理或者不取消事件,selector.select()方法不會(huì)阻塞) //sk.cancel(); //區(qū)分不同的事件做不同的動(dòng)作 if(sk.isAcceptable()){ //有連接請(qǐng)求事件//通過(guò)SelectionKey 獲取對(duì)應(yīng)的channelServerSocketChannel channel = (ServerSocketChannel) sk.channel();SocketChannel socketChannel = channel.accept();socketChannel.configureBlocking(false);//讀取數(shù)據(jù)內(nèi)容,bytebuffer大小注意消息邊界(一個(gè)字符串被分割讀取多次出來(lái)結(jié)果不準(zhǔn)確)ByteBuffer byteBuffer = ByteBuffer.allocate(1024);//將socketChannel綁定SelectorSelectionKey socketKey = socketChannel.register(selector,0,byteBuffer);//關(guān)注可讀事件socketKey.interestOps(SelectionKey.OP_READ); }else if(sk.isReadable()){//可讀事件try { //取到Channel SocketChannel socketChannel = (SocketChannel) sk.channel(); //獲取到綁定的ByteBuffer ByteBuffer byteBuffer = (ByteBuffer) sk.attachment(); int read = socketChannel.read(byteBuffer); //如果是正常斷開(kāi) read = -1 if(read == -1){//取消事件sk.cancel();continue; } byteBuffer.flip(); String str = StandardCharsets.UTF_8.decode(byteBuffer).toString(); System.out.println('服務(wù)端讀取到數(shù)據(jù)===========:' + str); //寫(xiě)數(shù)據(jù)回客戶端 ByteBuffer writeBuffer = StandardCharsets.UTF_8.encode('this is result'); socketChannel.write(writeBuffer); //如果數(shù)據(jù)一次沒(méi)寫(xiě)完關(guān)注可寫(xiě)事件進(jìn)行再次寫(xiě)入(大數(shù)據(jù)一次寫(xiě)不完的情況) if(writeBuffer.hasRemaining()){//關(guān)注可寫(xiě)事件,添加事件,用interestOps()方法獲取到之前的事件加上可寫(xiě)事件(類(lèi)似linux系統(tǒng)的賦權(quán)限 777)sk.interestOps(sk.interestOps() + SelectionKey.OP_WRITE);sk.attach(writeBuffer);//位運(yùn)算符也可以//sk.interestOps(sk.interestOps() | SelectionKey.OP_WRITE); }}catch (IOException e){ e.printStackTrace(); //客戶端異常斷開(kāi)連接 ,取消事件 sk.cancel();} }else if(sk.isWritable()){//取到ChannelSocketChannel socketChannel = (SocketChannel) sk.channel();//獲取到綁定的ByteBufferByteBuffer writeBuffer = (ByteBuffer) sk.attachment();socketChannel.write(writeBuffer);//如果全部寫(xiě)完,取消可寫(xiě)事件綁定,解除writeBuffer綁定if(!writeBuffer.hasRemaining()){ sk.attach(null); //取消可寫(xiě)事件 sk.interestOps(sk.interestOps() - SelectionKey.OP_WRITE);} }} }}

到此這篇關(guān)于java基礎(chǔ)之NIO介紹及使用的文章就介紹到這了,更多相關(guān)Java NIO詳解內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美亚洲网站| 国产精品a级| 日本午夜精品一区二区三区电影 | 精品一区视频| 久久99精品久久久久久园产越南| 欧美亚洲综合视频| 国产精品亚洲欧美| 麻豆成人91精品二区三区| 欧美激情网址| 欧美韩日一区| 国产91欧美| 秋霞影视一区二区三区| 婷婷色综合网| 欧美在线亚洲| 亚洲欧美日本日韩| 亚洲精品国模| 国产精品嫩草影院在线看| 国产乱码精品一区二区亚洲| 国产精品啊啊啊| 电影91久久久| 久久久久久久久丰满| 91成人精品| 日韩在线电影| 麻豆一区二区三区| 九九色在线视频| 99国产精品久久久久久久| 四虎精品永久免费| 国产精品久久久久久av公交车| 精品亚洲精品| 美女网站一区| 日韩精品视频一区二区三区| 久久99蜜桃| 深夜视频一区二区| 免费日本视频一区| 美女精品视频在线| 欧美日韩在线观看视频小说| 免费日韩一区二区| 国产精品视频一区二区三区综合| av高清不卡| 手机精品视频在线观看| 国产精品115| 亚洲高清不卡| 日韩av三区| 国产传媒在线| 亚洲一二av| 一区二区精品伦理...| 午夜欧美精品| 国产精品一线| 欧美日韩国产高清电影| 欧美综合社区国产| 日韩在线观看不卡| 欧美视频二区| 影院欧美亚洲| 国产精品一区二区免费福利视频 | 日本综合视频| 91av亚洲| 日韩一二三区在线观看| 欧美激情另类| 亚洲精品第一| 久久婷婷激情| 久久黄色影视| 欧美理论视频| 精品久久久亚洲| 亚洲精品一区二区在线播放∴| 精品国产亚洲一区二区三区在线| 亚洲国产日韩欧美在线| 欧美亚洲一级| 欧美日韩国产综合网| 国产精品白丝一区二区三区| 黄色不卡一区| 欧美国产中文高清| 亚洲有吗中文字幕| 伊人久久大香线蕉av不卡| 久久wwww| 日韩一区二区三区精品 | 中文字幕日韩亚洲| 久久一区二区三区电影| 欧美成人一二区| 综合一区二区三区| 蜜桃视频欧美| 四虎国产精品免费观看| 日韩国产欧美一区二区三区| 欧美亚洲精品在线| 精品国产黄a∨片高清在线| 日韩精品久久久久久| 好吊视频一区二区三区四区| 日韩a一区二区| 国产日韩一区二区三区在线播放 | 久久一级电影| 91欧美日韩| 麻豆精品一区二区综合av| 亚洲3区在线| 亚洲在线免费| 亚洲91久久| 伊人久久在线| 久久天堂影院| 日本欧美韩国一区三区| 蜜桃免费网站一区二区三区| 欧美中文一区二区| 蜜臀久久精品| 日韩国产一区二区| 91免费精品| 日韩av有码| 精品国产欧美日韩一区二区三区| 国产精区一区二区| 欧美亚洲三级| 欧美日韩亚洲国产精品| 免费成人在线影院| 亚洲精品91| 亚洲午夜精品久久久久久app| 日本免费一区二区三区四区| 国产精品日韩精品中文字幕| 日韩欧美久久| 欧美伊人久久| 国产图片一区| 国产精品自拍区| 国产伦精品一区二区三区视频| 国产一卡不卡| 欧美国产视频| 成人在线超碰| 国产精品亚洲一区二区三区在线观看| 97精品国产一区二区三区| 四季av一区二区凹凸精品| 高清在线一区| 国产成人免费| 日韩综合精品| 欧美日韩在线观看首页| 91精品xxx在线观看| 91精品婷婷色在线观看| 欧美91精品| 午夜在线视频一区二区区别| 亚洲欧美日韩精品一区二区| 亚洲资源网站| 国产剧情在线观看一区| 美女国产精品久久久| 国产91在线播放精品| 久久久成人网| 亚洲欧洲一区| 亚洲日本三级| 国产无遮挡裸体免费久久| 久久香蕉网站| 国产福利片在线观看| 久久九九精品| 久久国产精品99国产| 日韩区一区二| 精品亚洲二区| 久久国产影院| 免费在线视频一区| 国产欧美日韩综合一区在线播放| 久久久免费人体| 91精品一区二区三区综合在线爱| 国产精品日韩| 欧美日本久久| 国产理论在线| 亚洲少妇一区| 欧美一区不卡| 精品在线网站观看| 欧美理论视频| 日韩毛片一区| 在线天堂资源www在线污| 欧美日韩在线二区| 亚久久调教视频| 国产在线观看91一区二区三区| 欧美/亚洲一区| 91麻豆精品| 麻豆91小视频| 中文在线一区| 国产精品免费99久久久| 女生影院久久| 久久国产88| 美女国产一区二区三区| 99精品电影| 日本午夜精品一区二区三区电影| 美女av在线免费看| 美美哒免费高清在线观看视频一区二区| 欧美一级二区| 一区二区三区视频免费观看| 日韩激情av在线| 精品捆绑调教一区二区三区| 免费黄网站欧美| sm捆绑调教国产免费网站在线观看| 蘑菇福利视频一区播放| 麻豆极品一区二区三区| 日韩天堂av| 麻豆一区二区在线| 亚洲神马久久| 欧美国产专区| 丝袜美腿一区二区三区| 福利欧美精品在线| 亚洲深夜福利在线观看| 久久精品国产网站| 久久福利精品| 国产一区二区三区不卡av| 石原莉奈一区二区三区在线观看| 高清久久一区| 五月国产精品| 不卡中文一二三区| 免费在线欧美黄色| 亚洲精品伊人| 国精品一区二区|