第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用nio 中 selectionKey.isReadable()造成 死循環(huán)

使用nio 中 selectionKey.isReadable()造成 死循環(huán)

慕粉4241372 2018-08-09 14:01:17
@Test public?void?clientSocket()?throws?IOException?{ ????SocketChannel?socketChannel?=?SocketChannel.open(new?InetSocketAddress("localhost",?9999)); ????socketChannel.configureBlocking(false); ????ByteBuffer?byteBuffer?=?ByteBuffer.allocate(1024); ????byteBuffer.put("你好好好好".getBytes()); ????byteBuffer.flip(); ????socketChannel.write(byteBuffer); ????byteBuffer.clear(); ????socketChannel.shutdownOutput(); ????socketChannel.close(); } @Test public?void?serverSocket()?throws?IOException?{ ????ServerSocketChannel?serverSocketChannel?=?ServerSocketChannel.open(); ????serverSocketChannel.configureBlocking(false); ????serverSocketChannel.bind(new?InetSocketAddress(9999)); ????Selector?selector?=?Selector.open(); ????//?注冊(cè)連接事件 ????serverSocketChannel.register(selector,?SelectionKey.OP_ACCEPT); ????while?(selector.select()?>?0)?{ ????????Set<SelectionKey>?selectionKeySet?=?selector.selectedKeys(); ????????System.out.println(selectionKeySet.size()); ????????Iterator<SelectionKey>?iterator?=?selectionKeySet.iterator(); ????????while?(iterator.hasNext())?{ ????????????SelectionKey?selectionKey?=?iterator.next(); ????????????if?(selectionKey.isAcceptable())?{ ????????????????System.out.println("accept====="); ????????????????SocketChannel?socketChannel?=?serverSocketChannel.accept(); ????????????????socketChannel.configureBlocking(false); ????????????????socketChannel.register(selector,?SelectionKey.OP_READ); ????????????}?else?if?(selectionKey.isReadable())?{ ???????????????????? ????????????????System.out.println("read====="); ????????????????SocketChannel?socketChannel?=?(SocketChannel)?selectionKey.channel(); ????????????????ByteBuffer?byteBuffer?=?ByteBuffer.allocate(1024); ????????????????socketChannel.read(byteBuffer); ????????????????byteBuffer.flip(); ????????????????System.out.println(new?String(byteBuffer.array())?+?":::::::::::::::::::"); ????????????????byteBuffer.clear(); ????????????} ????????????iterator.remove(); ????????????System.out.println(selectionKeySet.size()); ????????} ????} }在client結(jié)束后,我末尾打印的selectionKeySet.size()方法,本來(lái)是0,結(jié)果循環(huán)一遍siez卻變成1了,仍然會(huì)不停的進(jìn)入selectionKey.isReadable()方法,導(dǎo)致死循環(huán)最后iterator應(yīng)該是移除掉了,但集合里面總有一個(gè)SelectionKeyImp的對(duì)象,這個(gè)是哪里來(lái)的?我末尾打印的selectionKeySet.size()方法,本來(lái)是0,結(jié)果循環(huán)一遍又這是控制臺(tái):1 [sun.nio.ch.SelectionKeyImpl@4b9e13df] accept===== 0 1 [sun.nio.ch.SelectionKeyImpl@2b98378d] read===== 你好好好好?? 0 1 [sun.nio.ch.SelectionKeyImpl@2b98378d] read=====??????? ???? 0 1 [sun.nio.ch.SelectionKeyImpl@2b98378d] read=====??????? ? 0 1 [sun.nio.ch.SelectionKeyImpl@2b98378d] read=====????? 0 1 [sun.nio.ch.SelectionKeyImpl@2b98378d] read=====
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 3021 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)