设为首页 加入收藏

TOP

高并发Java(8):NIO和AIO(三)
2017-09-30 13:37:08 】 浏览:2002
Tags:并发 Java NIO AIO
ovider.provider().openSelector(); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); InetSocketAddress isa = new InetSocketAddress(8000); ssc.socket().bind(isa); // 注册感兴趣的事件,此处对accpet事件感兴趣 SelectionKey acceptKey = ssc.register(selector, SelectionKey.OP_ACCEPT); for (;;) { selector.select(); Set readyKeys = selector.selectedKeys(); Iterator i = readyKeys.iterator(); long e = 0; while (i.hasNext()) { SelectionKey sk = (SelectionKey) i.next(); i.remove(); if (sk.isAcceptable()) { doAccept(sk); } else if (sk.isValid() && sk.isReadable()) { if (!geym_time_stat.containsKey(((SocketChannel) sk .channel()).socket())) { geym_time_stat.put( ((SocketChannel) sk.channel()).socket(), System.currentTimeMillis()); } doRead(sk); } else if (sk.isValid() && sk.isWritable()) { doWrite(sk); e = System.currentTimeMillis(); long b = geym_time_stat.remove(((SocketChannel) sk .channel()).socket()); System.out.println("spend:" + (e - b) + "ms"); } } } } private void doWrite(SelectionKey sk) { // TODO Auto-generated method stub SocketChannel channel = (SocketChannel) sk.channel(); EchoClient echoClient = (EchoClient) sk.attachment(); LinkedList<ByteBuffer> outq = echoClient.getOutputQueue(); ByteBuffer bb = outq.getLast(); try { int len = channel.write(bb); if (len == -1) { disconnect(sk); return; } if (bb.remaining() == 0) { outq.removeLast(); } } catch (Exception e) { // TODO: handle exception disconnect(sk); } if (outq.size() == 0) { sk.interestOps(SelectionKey.OP_READ); } } private void doRead(SelectionKey sk) { // TODO Auto-generated method stub SocketChannel channel = (SocketChannel) sk.channel(); ByteBuffer bb = ByteBuffer.allocate(8192); int len; try { len = channel.read(bb); if (len < 0) { disconnect(sk); return; } } catch (Exception e) { // TODO: handle exception disconnect(sk); return; } bb.flip(); tp.execute(new HandleMsg(sk, bb)); } private void disconnect(SelectionKey sk) { // TODO Auto-generated method stub //省略略干关闭操作 } private void doAccept(SelectionKey sk) { // TODO Auto-generated method stub ServerSocketChannel server = (ServerSocketChannel) sk.channel(); SocketChannel clientChannel; try { clientChannel = server.accept(); clientChannel.configureBlocking(false); SelectionKey clientKey = clientChannel.register(selector, SelectionKey.OP_READ); EchoClient echoClinet = new EchoClient(); clientKey.attach(echoClinet); InetAddress clientAddress = clientChannel.socket().getInetAddress(); System.out.println("Accepted connection from " + clientAddress.getHostAddress()); } catch (Exception e) { // TODO: handle exception } } public static void main(String[] args) { // TODO Auto-generated method stub MultiThreadNIOEchoServer echoServer = new MultiThreadNIOEchoServer();
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring MVC REST异常处理最佳实践.. 下一篇高并发Java(9):锁的优化和注意..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目