一个简单的 IO 任务。
服务端
| 12
 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
 
 | package com.redisc;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.channel.socket.nio.NioSocketChannel;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.IOException;
 import java.nio.charset.Charset;
 
 @Slf4j
 public class Run {
 
 public static void main(String[] args) throws IOException {
 new ServerBootstrap()
 .group(new NioEventLoopGroup())
 .channel(NioServerSocketChannel.class)
 .childHandler(new ChannelInitializer<NioSocketChannel>() {
 
 @Override
 protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
 nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
 ByteBuf buffer = (ByteBuf) msg;
 log.debug(buffer.toString(Charset.defaultCharset()));
 }
 });
 }
 
 }).bind(8000);
 }
 
 }
 
 | 
客户端
| 12
 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
 
 | package com.redisc;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioSocketChannel;
 import io.netty.handler.codec.string.StringEncoder;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
 
 public class Client {
 public static void main(String[] args) throws IOException, InterruptedException {
 Channel channel = new Bootstrap()
 .group(new NioEventLoopGroup())
 .channel(NioSocketChannel.class)
 .handler(new ChannelInitializer<NioSocketChannel>() {
 @Override
 protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
 nioSocketChannel.pipeline().addLast(new StringEncoder());
 
 }
 }).connect(new InetSocketAddress("localhost", 8000))
 .sync().channel();
 channel.writeAndFlush("hello");
 }
 }
 
 | 
在 26 处打个断点。
运行
- 打开服务端
- debug模式开启- client「- debug模式是- thread模式」- 
- 在 26处,不断的发送信息,然后关闭客户端,重启一个客户端,发送信息,重复3次
 
输出
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | 21:38:33.198 [nioEventLoopGroup-2-2] DEBUG com.redisc.Run - hello 21:38:37.001 [nioEventLoopGroup-2-2] DEBUG com.redisc.Run - hello
 21:38:37.851 [nioEventLoopGroup-2-2] DEBUG com.redisc.Run - hello
 21:38:40.985 [nioEventLoopGroup-2-2] DEBUG com.redisc.Run - 234
 21:38:44.990 [nioEventLoopGroup-2-2] DEBUG com.redisc.Run - hello
 21:38:54.830 [nioEventLoopGroup-2-3] DEBUG com.redisc.Run - 123
 21:38:57.230 [nioEventLoopGroup-2-3] DEBUG com.redisc.Run - 123
 21:38:57.499 [nioEventLoopGroup-2-3] DEBUG com.redisc.Run - 123
 21:38:57.771 [nioEventLoopGroup-2-3] DEBUG com.redisc.Run - 123
 21:39:02.297 [nioEventLoopGroup-2-3] DEBUG com.redisc.Run - hello
 21:39:09.959 [nioEventLoopGroup-2-4] DEBUG com.redisc.Run - hello
 21:39:10.785 [nioEventLoopGroup-2-4] DEBUG com.redisc.Run - hello
 
 | 
可以发现,每一个客户端发送的信息都是由一个 eventloop 里面的一个单独的处理器处理。