我创建了一个具有多个工作线程的 Netty 服务器,以检查线程数量的增加如何改变吞吐量。这是我使用的代码。它是编写和回显服务器的稍微修改版本,可以在 Netty 网站中找到。
Echo服务器计算
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class EchoServerCompute {
private int port;
public EchoServerCompute(int port) {
this.port = port;
}
public void run(int threadCount) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(threadCount);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerComputeHandler());
} …
Run Code Online (Sandbox Code Playgroud) 我创建了一个小型 Netty 服务器来计算 BigInteger 的阶乘并发送结果。代码如下。
因子.java
public class Factorial {
private int port;
public Factorial(int port) {
this.port = port;
}
public void run(int threadcount) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(threadcount);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new FactorialHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public …
Run Code Online (Sandbox Code Playgroud) 我通过打印 JVM 参数来检查 JVM 上的默认 MaxMetaspaceSize,如下所示
java -XX:+PrintFlagsFinal -version | grep MaxMetaspaceSize
uintx MaxMetaspaceSize = 18446744073709547520 {product}
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
Run Code Online (Sandbox Code Playgroud)
鉴于这些 JVM 参数值以字节为单位显示,默认 MaxMetaspaceSize 约为 18 艾字节!
有人可以解释一下为什么默认的 MaxMetaspaceSize 值这么大吗?