标签:相对 并发 阻塞 font protoc 抽象 open 并且 handle
1 import java.io.IOException; 2 import java.net.InetSocketAddress; 3 import java.nio.channels.SelectionKey; 4 import java.nio.channels.Selector; 5 import java.nio.channels.ServerSocketChannel; 6 import java.util.Iterator; 7 8 public class TCPServerSelector{ 9 //缓冲区的长度 10 private static final int BUFSIZE = 256; 11 //select方法等待信道准备好的最长时间 12 private static final int TIMEOUT = 3000; 13 public static void main(String[] args) throws IOException { 14 if (args.length < 1){ 15 throw new IllegalArgumentException("Parameter(s): <Port> ..."); 16 } 17 //创建一个选择器 18 Selector selector = Selector.open(); 19 for (String arg : args){ 20 //实例化一个信道 21 ServerSocketChannel listnChannel = ServerSocketChannel.open(); 22 //将该信道绑定到指定端口 23 listnChannel.socket().bind(new InetSocketAddress(Integer.parseInt(arg))); 24 //配置信道为非阻塞模式 25 listnChannel.configureBlocking(false); 26 //将选择器注册到各个信道 27 listnChannel.register(selector, SelectionKey.OP_ACCEPT); 28 } 29 //创建一个实现了协议接口的对象 30 TCPProtocol protocol = new EchoSelectorProtocol(BUFSIZE); 31 //不断轮询select方法,获取准备好的信道所关联的Key集 32 while (true){ 33 //一直等待,直至有信道准备好了I/O操作 34 if (selector.select(TIMEOUT) == 0){ 35 //在等待信道准备的同时,也可以异步地执行其他任务, 36 //这里只是简单地打印"." 37 System.out.print("."); 38 continue; 39 } 40 //获取准备好的信道所关联的Key集合的iterator实例 41 Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator(); 42 //循环取得集合中的每个键值 43 while (keyIter.hasNext()){ 44 SelectionKey key = keyIter.next(); 45 //如果服务端信道感兴趣的I/O操作为accept 46 if (key.isAcceptable()){ 47 protocol.handleAccept(key); 48 } 49 //如果客户端信道感兴趣的I/O操作为read 50 if (key.isReadable()){ 51 protocol.handleRead(key); 52 } 53 //如果该键值有效,并且其对应的客户端信道感兴趣的I/O操作为write 54 if (key.isValid() && key.isWritable()) { 55 protocol.handleWrite(key); 56 } 57 //这里需要手动从键集中移除当前的key 58 keyIter.remove(); 59 } 60 } 61 } 62 }
1 public static void main(String[] args) throws IOException { 2 3 AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0000)); 4 server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() { 5 final ByteBuffer buffer = ByteBuffer.allocate(1024); 6 7 @Override 8 public void completed(AsynchronousSocketChannel result, Object attachment) { 9 System.out.println(Thread.currentThread().getName()); 10 Future<Integer> writeResult = null; 11 try { 12 buffer.clear(); 13 result.read(buffer).get(100, TimeUnit.SECONDS); 14 buffer.flip(); 15 writeResult = result.write(buffer); 16 } catch (InterruptedException | ExecutionException e) { 17 e.printStackTrace(); 18 } catch (TimeoutException e) { 19 e.printStackTrace(); 20 } finally { 21 try { 22 server.accept(null, this); 23 writeResult.get(); 24 result.close(); 25 } catch (Exception e) { 26 System.out.println(e.toString()); 27 } 28 } 29 } 30 31 @Override 32 public void failed(Throwable exc, Object attachment) { 33 System.out.println("failed: " + exc); 34 } 35 }); 36 37 }
标签:相对 并发 阻塞 font protoc 抽象 open 并且 handle
原文地址:https://www.cnblogs.com/756623607-zhang/p/9181003.html