簡體   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