本文共 796 字,大约阅读时间需要 2 分钟。
java.nio.channels.IllegalBlockingModeException
- class Handler {
- public Handler(Selector selector, SocketChannel socketChannel) throws InterruptedException {
- try {
- SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ);
- } catch (IOException e) {
-
- e.printStackTrace();
- }
-
-
-
-
-
- socketChannelMap.put(socketChannel.hashCode(), socketChannel);
-
- System.out.println("Handler end" + socketChannel.hashCode() + "," + socketChannelMap.size());
- }
- }
发生错误语句: - SelectionKey key = socketChannel.register(selector, SelectionKey.OP_READ);
错误原因: 必须设置通道为 非阻塞,才能向 Selector 注册。
- socketChannel.configureBlocking(false);
注意参数值,false 为 非阻塞,true 为 阻塞。
转载地址:http://qxdfo.baihongyu.com/