繁体   English   中英

Netty客户端相对于I / O客户端的好处?

[英]Benefits of Netty Client over I/O Client?

我想知道是否可以通过实施Netty Client保存我的应用程序线程。

我编写了一个演示客户端,请找到以下代码。 期望单个线程可以连接到不同的端口可以有效地处理它们,但是我错了。 Netty为每个线程创建连接。

public class NettyClient {
    public static void main(String[] args) {
    Runnable runA = new Runnable() {
        public void run() {
            Connect(5544);
        }
    };

    Thread threadA = new Thread(runA, "threadA");
    threadA.start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    Runnable runB = new Runnable() {
        public void run() {
            Connect(5544);
        }
    };

    Thread threadB = new Thread(runB, "threadB");
    threadB.start();

}

static ClientBootstrap bootstrap = null;
static NettyClient ins = new NettyClient();
public NettyClient() {

    bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    /*
     * ClientBootstrap A helper class which creates a new client-side
     * Channel and makes a connection attempt.
     * 
     * NioClientSocketChannelFactory A ClientSocketChannelFactory which
     * creates a client-side NIO-based SocketChannel. It utilizes the
     * non-blocking I/O mode which was introduced with NIO to serve many
     * number of concurrent connections efficiently
     * 
     * There are two types of threads :Boss thread Worker threads Boss
     * Thread passes control to worker thread.
     */
    // Configure the client.

    ChannelGroup channelGroup = new DefaultChannelGroup(NettyClient.class.getName());
    // Only 1 thread configured but still aceepts threadA and Thread B
    // connection
    OrderedMemoryAwareThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor(
            1, 1048576, 1073741824, 1, TimeUnit.MILLISECONDS,
            new NioDataSizeEstimator(), new NioThreadFactory("NioPipeline"));

    bootstrap.setPipelineFactory(new NioCommPipelineFactory(channelGroup,
            pipelineExecutor));

    // bootstrap.setPipelineFactory(new
    // BackfillClientSocketChannelFactory());
    bootstrap.setOption("child.tcpNoDelay", true);

    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("readWriteFair", true);
}

public static NettyClient getins() {
    return ins;
}

public static void Connect(int port) {
    ChannelFuture future = bootstrap
            .connect(new InetSocketAddress("localhost", port));

    Channel channel = future.awaitUninterruptibly().getChannel();
    System.out.println(channel.getId());
    channel.getCloseFuture().awaitUninterruptibly();
}

}

现在我想知道使用Netty客户端有什么好处? 是否保存线程?

Netty保存线程。 当同步等待打开和关闭连接(调用awaitUninterruptible())时,您的NettyClient浪费线程。

顺便说一句,您的客户有多少个连接? 也许使用经典的同步每连接一个线程方法就足够了? 通常我们必须将线程保存在服务器端。

Netty允许您使用少量线程处理数千个连接 在客户端应用程序中使用时,它允许少数线程与服务器建立数千个并发连接。

您已将sleep()放入线程中。 我们绝不能阻止Netty工人/老板线程。 即使需要执行一次一次性阻止操作,也必须将其卸载给另一位执行者。 Netty使用NIO,同一线程可用于创建新连接,而较早的连接在其输入缓冲区中获取一些数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM