BIO,NIO,AIO Netty
- [BIO,NIO,AIO ](#bionioaio-)
- [1. BIO \(Blocking I/O\)](#1-bio-blocking-io)
- [1.1 BIO](#11--bio)
- [1.2 IO](#12--io)
- [1.3 ](#13-)
- [1.4 ](#14-)
- [2. NIO \(New I/O\)](#2-nio-new-io)
- [2.1 NIO ](#21-nio-)
- [2.2 NIO/NIOIO](#22-nionioio)
- [1)Non-blocking IOIO](#1non-blocking-ioio)
- [2)Buffer\(\)](#2buffer)
- [3)Channel \(\)](#3channel-)
- [4)Selectors\(\)](#4selectors)
- [2.3 NIO ](#23-nio-)
- [2.4 NIO](#24-nio)
- [2.5 ](#25-)
- [3. AIO \(Asynchronous I/O\)](#3-aio-asynchronous-io)
- [](#)
# BIO,NIO,AIO
Java BIONIO AIO Java IO API JavaAPI
BIO,NIO,AIO
****
- ****
- ****
****
- ****
- ****
************
## 1. BIO (Blocking I/O)
I/O
### 1.1 BIO
BIO()

**BIO ** Acceptor `while(true)` `accept()`
**BIO ** `socket.accept()``socket.read()``socket.write()` **** **** `FixedThreadPool` N():M()I/ON M" BIO"
****
Java Linux
### 1.2 IO
I/OMNMN.
IO()

I/O Socket Taskjava.lang.RunnableJDK N
I/OBIO
### 1.3
BIO"+:hello world"
[https://www.jianshu.com/p/a4e03835921a](https://www.jianshu.com/p/a4e03835921a)
****
```java
/**
*
* @author
* @date 20181014
* @Description:
*/
public class IOClient {
public static void main(String[] args) {
// TODO
new Thread(() -> {
try {
Socket socket = new Socket("127.0.0.1", 3333);
while (true) {
try {
socket.getOutputStream().write((new Date() + ": hello world").getBytes());
Thread.sleep(2000);
} catch (Exception e) {
}
}
} catch (IOException e) {
}
}).start();
}
}
```
****
```java
/**
* @author
* @date 20181014
* @Description:
*/
public class IOServer {
public static void main(String[] args) throws IOException {
// TODO
ServerSocket serverSocket = new ServerSocket(3333);
//
new Thread(() -> {
while (true) {
try {
//
Socket socket = serverSocket.accept();
//
new Thread(() -> {
try {
int len;
byte[] data = new byte[1024];
InputStream inputStream = socket.getInputStream();
//
while ((len = inputStream.read(data)) != -1) {
System.out.println(new String(data, 0, len));
}
} catch (IOException e) {
}
}).start();
} catch (IOException e) {
}
}
}).start();
}
}
```
### 1.4
1000 I/O BIO I/O
## 2. NIO (New I/O)
### 2.1 NIO
NIOI/OJava 1.4 NIO java.nio Channel , SelectorBuffer
NIONNon-blockingNewI/O NIOBIO `Socket` `ServerSocket` `SocketChannel` `ServerSocketChannel` ,I/O NIO
### 2.2 NIO/NIOIO
NIO IO IO IO NIO 3/ NIO NIO
#### 1)Non-blocking IOIO
**IONIO**
Java NIOIObufferbuffer
Java IO `read()` `write()`
#### 2)Buffer()
**IO (Stream oriented) NIO (Buffer oriented)**
BufferNIOBufferI/OI/O Stream Stream Buffer NIO Buffer
NIO; NIO
ByteBuffer, ByteBuffer byte ByteBuffer,JavaBoolean
#### 3)Channel ()
NIO Channel
Buffer Buffer
#### 4)Selector ()
NIOIO

### 2.3 NIO
NIOIO Channel
-
-

### 2.4 NIO
NIO
- Channel()
- Buffer()
- Selector()
NIONIOAPI
### 2.5
[https://www.jianshu.com/p/a4e03835921a](https://www.jianshu.com/p/a4e03835921a)
IOClient.java NIO
```java
/**
*
* @author
* @date 2019221
* @Description: NIO
*/
public class NIOServer {
public static void main(String[] args) throws IOException {
// 1. serverSelector
// clientSelector IO 1w while
Selector serverSelector = Selector.open();
// 2. clientSelector
Selector clientSelector = Selector.open();
new Thread(() -> {
try {
// IO
ServerSocketChannel listenerChannel = ServerSocketChannel.open();
listenerChannel.socket().bind(new InetSocketAddress(3333));
listenerChannel.configureBlocking(false);
listenerChannel.register(serverSelector, SelectionKey.OP_ACCEPT);
while (true) {
// 1 1ms
if (serverSelector.select(1) > 0) {
Set set = serverSelector.selectedKeys();
Iterator keyIterator = set.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isAcceptable()) {
try {
// (1) clientSelector
SocketChannel clientChannel = ((ServerSocketChannel) key.channel()).accept();
clientChannel.configureBlocking(false);
clientChannel.register(clientSelector, SelectionKey.OP_READ);
} finally {
keyIterator.remove();
}
}
}
}
}
} catch (IOException ignored) {
}
}).start();
new Thread(() -> {
try {
while (true) {
// (2) 1 1ms
if (clientSelector.select(1) > 0) {
Set set = clientSelector.selectedKeys();
Iterator keyIterator = set.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isReadable()) {
try {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// (3) Buffer
clientChannel.read(byteBuffer);
byteBuffer.flip();
System.out.println(
Charset.defaultCharset().newDecoder().decode(byteBuffer).toString());
} finally {
keyIterator.remove();
key.interestOps(SelectionKey.OP_READ);
}
}
}
}
}
} catch (IOException ignored) {
}
}).start();
}
}
```
JDK NIO
- JDK NIO epoll bug cpu 100%
- NIO bug bug
Netty JDK NIO
### 3. AIO (Asynchronous I/O)
AIO NIO 2 Java 7 NIO NIO 2,IO IO
AIO IO NIO NIO IO NIO IO IO IO AIO IO IO[LinuxIO](https://mp.weixin.qq.com/s?__biz=Mzg3MjA4MTExMw==&mid=2247484746&idx=1&sn=c0a7f9129d780786cabfcac0a8aa6bb7&source=41#wechat_redirect)
AIO Netty AIO
##
- Netty
- https://zhuanlan.zhihu.com/p/23488863 ()