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

时间:2015-02-05 17:11:17

标签: java multithreading network-programming netty

我想知道是否可以通过实施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客户端有什么好处?它会保存线程吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

你已经在你的线程中放了sleep()。我们必须永远不会阻止Netty工作者/老板线程。即使需要执行任何一次性阻塞操作,也必须将其卸载到另一个执行器。 Netty使用NIO,同一个线程可以用于创建新连接,而早期的连接在其输入缓冲区中获取一些数据。