0%

java | 非阻塞模式

非阻塞模式。

引入自写的方法类。

下载 JAVA 文件

netty 包

1
2
3
4
5
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.86.Final</version>
</dependency>

服务端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

import static com.redisc.ByteBufferUtil.debugRead;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws IOException {
// 使用 nio 单线程
// bytebuffer
ByteBuffer buffer = ByteBuffer.allocate(16);
// 创建服务器
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);//将阻塞模式切换为非阻塞模式
//绑定端口
ssc.bind(new InetSocketAddress(8090));
//连接集合
List<SocketChannel> channelList = new ArrayList<>();
while (true) {
// accept 建立客户断连接,SocketChannel 用来与客户端通信
SocketChannel sc = ssc.accept(); // accept 是一个非阻塞方法,线程还会继续运行,如果没有连接,sc 是 null
if (sc != null) {
sc.configureBlocking(false);// 设置sc为非阻塞模式
channelList.add(sc);

}
for (SocketChannel channel : channelList) {
// 接收客户端发送的数据
int read = channel.read(buffer); // 非阻塞方法,如果没有读到数据 read 返回 0
if (read > 0) {
buffer.flip();
debugRead(buffer);
buffer.clear();
log.debug("after read...{}", channel);
}
}
}
}

}

客户端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.redisc;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class Client {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost", 8090));
System.out.println("waiting...");
}
}

验证

开启服务器。

客户端代码,在 11 行打断点。开启客户端,在 sc 变量上执行

多次执行

1
sc.write(Charset.defaultCharset().encode("hi"))

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
15:42:49.416 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [2]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 69 |hi |
+--------+-------------------------------------------------+----------------+
15:42:49.470 [main] DEBUG c.Test - after read...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:53153]
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [2]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 69 |hi |
+--------+-------------------------------------------------+----------------+
15:42:52.402 [main] DEBUG c.Test - after read...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:53153]
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [2]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 69 |hi |
+--------+-------------------------------------------------+----------------+
15:42:53.171 [main] DEBUG c.Test - after read...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:53153]
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [2]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 69 |hi |
+--------+-------------------------------------------------+----------------+
15:42:53.767 [main] DEBUG c.Test - after read...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:53153]
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [2]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 69 |hi |
+--------+-------------------------------------------------+----------------+
15:42:54.375 [main] DEBUG c.Test - after read...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8090 remote=/127.0.0.1:53153]

但是这样会导致,cpu 利用率一直是 100%

请我喝杯咖啡吧~