NIO 相关类都被放在 java.nio
包及子包下, 并对原 java.io
包中的很多类进行改写.
NIO 有三大核心部分: Channel
通道, Buffer
缓冲区, Selector
缓冲区.
通道可以理解为 BIO 中的 Socket
连接, 每个通道又对应一个缓存区, 在读/写数据时都是针对缓冲区进行操作.
NIO 是面向缓冲区或者面向块编程(数据都是按照块进行组织和管理). 数据读取到缓冲区, 需要时可以在缓冲区中前后移动, 这就增加了处理过程中的灵活性, 使用它可以提供非阻塞式的高伸缩网络.
Java NIO 的非阻塞模式, 使一个线程从某通道发送请求或者读取数据, 但是它仅能得到目前可用的数据, 如果目前没有数据可用时, 就什么都不会获取, 而不是保持线程阻塞, 所以直至数据变得可以读取之前, 该线程可以继续做其它的事情. 非阻塞写也是如此, 一个线程请求写入一些数据到某通道, 但不需要等待完全写入, 这个线程同时可以做别的事情.
通俗理解: NIO 是可以做到用一个线程来处理多个操作的. 假设有 10000 个请求过来, 根据实际情况, 可以分配 50 或 1000 个线程来处理. 不像之前的阻塞 IO 那样, 非得分配 10000 个.
举例说明 Buffer 使用
public class BasicBuffer {
public static void main(String[] args) {
// 创建一个 Buffer, 大小为 10, 即可以存放 10 个 int
IntBuffer intBuffer = IntBuffer.allocate(10);
// 向 Buffer 中存放数据.
intBuffer.put(1);
intBuffer.put(2);
intBuffer.put(3);
intBuffer.put(4);
intBuffer.put(5);
// 进行读写切换
intBuffer.flip();
// 从 Buffer 读取数据.
while (intBuffer.hasRemaining()) {
System.out.println(intBuffer.get());
}
}
}
服务端
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
public class NIOServer {
public static void main(String[] args) throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
Selector selector = Selector.open();
//serverSocketChannel.socket().bind(new InetSocketAddress(6666));
serverSocketChannel.bind(new InetSocketAddress(6666));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
if (selector.select(1000) == 0) {
continue;
}
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
if(selectionKey.isAcceptable()) {
System.out.println("~~~~~~~~~~~~~");
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null){
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
}
}
if (selectionKey.isReadable()) {
System.out.println("++++++++++++++++");
SocketChannel channel = (SocketChannel) selectionKey.channel();
ByteBuffer src = ByteBuffer.allocate(1024);
channel.read(src);
System.out.println(new String(src.array()));
}
iterator.remove();
}
}
}
}
讨论
不需要等待完全写入是什么意思?
用 FileOutputStream
举例, 调用 write
方法写数据时, 会将数据写到对应文件中.
在没有将数据写完之前 write
方法, 是无法返回, 这时当前线程是无法做其它事情的.
后面会说关于 NIO 方面的实现.