Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

线程这样加下去,会不会有问题 #27

Open
burningtomato opened this issue May 19, 2022 · 4 comments
Open

线程这样加下去,会不会有问题 #27

burningtomato opened this issue May 19, 2022 · 4 comments

Comments

@burningtomato
Copy link

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吗?这是在频繁的新建和销毁线程吗?

@aLittleGreens
Copy link
Owner

aLittleGreens commented May 19, 2022 via email

@burningtomato
Copy link
Author

Thanks for your message

我也希望这个功能做的完美,加油

@burningtomato
Copy link
Author

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个小时候的部分日志

@burningtomato
Copy link
Author

group = new NioEventLoopGroup(1);
心跳包的话,一般不需要太耗性能,给一个线程慢慢的跑着就很满足了
如果设置成0,会默认跑出系统能承受的线程范围

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants