NettyDemo
NettyDemo copied to clipboard
线程这样加下去,会不会有问题
2022-05-19 10:13:26.146 2313-8729/littlegreens.nettydemo I/System.out: 线程ID=6327 线程活跃数量:11 2022-05-19 10:13:26.146 2313-8729/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-645-1 2022-05-19 10:13:31.162 2313-8729/littlegreens.nettydemo I/System.out: 线程ID=6327 线程活跃数量:3 2022-05-19 10:13:31.162 2313-8729/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-645-1 2022-05-19 10:13:31.163 2313-2380/littlegreens.nettydemo I/System.out: 线程ID=109 线程活跃数量:3 2022-05-19 10:13:31.163 2313-2380/littlegreens.nettydemo I/System.out: 线程名字:client-Netty 2022-05-19 10:13:31.193 2313-8739/littlegreens.nettydemo I/System.out: 线程ID=6337 线程活跃数量:11 2022-05-19 10:13:31.193 2313-8739/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-646-1 2022-05-19 10:13:36.203 2313-8739/littlegreens.nettydemo I/System.out: 线程ID=6337 线程活跃数量:3 2022-05-19 10:13:36.203 2313-8739/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-646-1 2022-05-19 10:13:36.205 2313-2380/littlegreens.nettydemo I/System.out: 线程ID=109 线程活跃数量:3 2022-05-19 10:13:36.205 2313-2380/littlegreens.nettydemo I/System.out: 线程名字:client-Netty 2022-05-19 10:13:36.237 2313-8749/littlegreens.nettydemo I/System.out: 线程ID=6347 线程活跃数量:11 2022-05-19 10:13:36.237 2313-8749/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-647-1 2022-05-19 10:13:41.548 2313-8749/littlegreens.nettydemo I/System.out: 线程ID=6347 线程活跃数量:3 2022-05-19 10:13:41.548 2313-8749/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-647-1 2022-05-19 10:13:41.550 2313-2380/littlegreens.nettydemo I/System.out: 线程ID=109 线程活跃数量:3 2022-05-19 10:13:41.550 2313-2380/littlegreens.nettydemo I/System.out: 线程名字:client-Netty 2022-05-19 10:13:41.574 2313-8759/littlegreens.nettydemo I/System.out: 线程ID=6357 线程活跃数量:11 2022-05-19 10:13:41.574 2313-8759/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-648-1 2022-05-19 10:13:46.587 2313-8759/littlegreens.nettydemo I/System.out: 线程ID=6357 线程活跃数量:3 2022-05-19 10:13:46.587 2313-8759/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-648-1 2022-05-19 10:13:46.589 2313-2380/littlegreens.nettydemo I/System.out: 线程ID=109 线程活跃数量:3 2022-05-19 10:13:46.589 2313-2380/littlegreens.nettydemo I/System.out: 线程名字:client-Netty 2022-05-19 10:13:46.612 2313-8769/littlegreens.nettydemo I/System.out: 线程ID=6367 线程活跃数量:11 2022-05-19 10:13:46.612 2313-8769/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-649-1 2022-05-19 10:13:51.646 2313-8769/littlegreens.nettydemo I/System.out: 线程ID=6367 线程活跃数量:3 2022-05-19 10:13:51.646 2313-8769/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-649-1 2022-05-19 10:13:51.647 2313-2380/littlegreens.nettydemo I/System.out: 线程ID=109 线程活跃数量:3 2022-05-19 10:13:51.647 2313-2380/littlegreens.nettydemo I/System.out: 线程名字:client-Netty 2022-05-19 10:13:51.660 2313-8779/littlegreens.nettydemo I/System.out: 线程ID=6377 线程活跃数量:11 2022-05-19 10:13:51.660 2313-8779/littlegreens.nettydemo I/System.out: 线程名字:nioEventLoopGroup-650-1
以上是我在提供的demo里打出的线程情况;这是不断的在创建线程,有销毁吗;这个数量一直在增加;会引起经常的GC吗?这是在频繁的新建和销毁线程吗?
Thanks for your message
Thanks for your message
我也希望这个功能做的完美,加油
Thanks for your message
private void connectServer() {
synchronized (NettyTcpClient.this) {
ChannelFuture channelFuture = null;
if (!isConnect) {
isConnecting = true;
group = new NioEventLoopGroup();<--这个地方,不应该一直去new的,我查看了其他的方式;只要没有一直创建这个对象,线程数量就能很好的控制在一个范围内,比方说10;
以下是我在NettyHeartbeatDemo运行时,得到的日志: 线程id=14 线程名字:nioEventLoopGroup-2-1 活跃线程:10 10s 之后尝试重新连接服务器... --- Server is active --- 线程id=15 线程名字:nioEventLoopGroup-2-2 活跃线程:10 --- Server is inactive --- 线程id=15 线程名字:nioEventLoopGroup-2-2 活跃线程:10 10s 之后尝试重新连接服务器... --- Server is active --- 线程id=16 线程名字:nioEventLoopGroup-2-3 活跃线程:10 --- Server is inactive --- 线程id=16 线程名字:nioEventLoopGroup-2-3 活跃线程:10 10s 之后尝试重新连接服务器... --- Server is active --- 线程id=17 线程名字:nioEventLoopGroup-2-4 活跃线程:10 --- Server is inactive --- 这是运行了2个小时候的部分日志
group = new NioEventLoopGroup(1); 心跳包的话,一般不需要太耗性能,给一个线程慢慢的跑着就很满足了 如果设置成0,会默认跑出系统能承受的线程范围