java阻塞循环,java 非阻塞io 即nio无限循环有关问题,困扰很久了,csdn版主、 ,开了一个贴,三天没有人回答,希望有高手帮助,拜谢了...

java 非阻塞io 即nio无限循环问题,困扰很久了,csdn版主、 高手请进,开了一个贴,三天没有人回答,希望有高手帮助,拜谢了

问题描述如下:

鄙人在写nio简单例子的时候出现了很奇怪的问题,服务器端部分代码如下:

while (selector.select() > 0)

{

Set readyKeys = selector.selectedKeys();

Iterator it = readyKeys.iterator();

System.out.println("while out");

while (it.hasNext())

{

System.out.println("while in");

SelectionKey key = (SelectionKey) it.next();

it.remove();

if (key.isAcceptable())

{

System.out.println("Key is Acceptable");

socket = (SocketChannel) ssc.accept();

socket.configureBlocking(false);

socket.register(selector,SelectionKey.OP_READ|SelectionKey.OP_WRITE);

}

if (key.isReadable()) {

System.out.println("Key is readable");

socket = (SocketChannel) key.channel();

ByteBuffer buf = ByteBuffer.allocate(25);

buf.clear();

socket.read(buf);

buf.flip();

Charset charset = Charset.forName("us-ascii");

CharsetDecoder decoder = charset.newDecoder();

CharBuffer charBuffer = decoder.decode(buf);

String result = charBuffer.toString();

System.out.println("Receive Data:" + result);

key.cancel();//如果这个注释的话,就会进行无限循环

}

}

客户端代码:

public static int sendMessage(SocketChannel client) {

String msg = null;

ByteBuffer bytebuf=ByteBuffer.allocate(1024);

int nBytes = 0;

try {

msg = "It's message from client!";

System.out.println("msg is "+msg);

bytebuf.clear();

bytebuf.put(msg.getBytes());

bytebuf.flip();

nBytes = client.write(bytebuf);

System.out.println("It's message from client!".getBytes().length);

client.close();

} catch (IOException e) {

e.printStackTrace();

}

return nBytes;

}

客户端和服务器端的部分代码就是上面的,客户端发送数据给服务器端,服务器按道理执行了

SelectionKey key = (SelectionKey) it.next();

it.remove();的代码之后注销了该键,如果执行完读操作之后,只要加上key.cancel()之后才可以正常读取一条数据。it.remove()之后为什么selector.select() > 0依然成立,运行代码的打印结果:

while out

while in

Key is readable

Receive Data:

while out

while in

Key is readable

Receive Data:

while out

while in

Key is readable

Receive Data:。。。。。

求高手解答。

------解决方案--------------------

引用:其实最想问的问题是,key.cancel()函数,其实没有把通道关闭是吧,这个函数具体作用是什么?看api没看懂。

public abstract void cancel()

使SelectionKey对象失效。

该方法把SelectionKey对象加入到与它关联的Selector对象的cancelled-keys集合中。当程序下一次执行Selector的select()方法时,该方法会把SelectionKey对象从3个集合中删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值