From 0630b8b10f1caeee53fb9553686242a57c9cd55d Mon Sep 17 00:00:00 2001 From: brianway <250902678@qq.com> Date: Mon, 24 Jun 2019 22:59:45 +0800 Subject: [PATCH] [add] add SelectorDemo for NIO --- .../java/nio/tutorial/SelectorDemo.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java diff --git a/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java new file mode 100644 index 0000000..99bf0a8 --- /dev/null +++ b/java-io/src/main/java/com/brianway/learning/java/nio/tutorial/SelectorDemo.java @@ -0,0 +1,64 @@ +package com.brianway.learning.java.nio.tutorial; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.util.Iterator; +import java.util.Set; + +/** + * Full Selector Example + * 启动后,浏览器输入 localhost:9999 + * + * @auther brian + * @since 2019/6/24 22:29 + */ +public class SelectorDemo { + + public static void main(String[] args) throws IOException { + Selector selector = Selector.open(); + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.socket().bind(new InetSocketAddress(9999)); + serverSocketChannel.configureBlocking(false); + + // SelectionKey key = + serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); + + while (true) { + + int readyChannels = selector.selectNow(); + + if (readyChannels == 0) { + // System.out.println("readyChannels == 0"); + continue; + } + + Set selectedKeys = selector.selectedKeys(); + + Iterator keyIterator = selectedKeys.iterator(); + + while (keyIterator.hasNext()) { + + SelectionKey key = keyIterator.next(); + + if (key.isAcceptable()) { + // a connection was accepted by a ServerSocketChannel. + System.out.println("accepted"); + } else if (key.isConnectable()) { + // a connection was established with a remote server. + System.out.println("connectable"); + } else if (key.isReadable()) { + // a channel is ready for reading + System.out.println("ready"); + } else if (key.isWritable()) { + // a channel is ready for writing + System.out.println("writable"); + } + + keyIterator.remove(); + } + } + } +}