2015-02-05 65 views
0

我想知道是否可以通過實現Netty客戶端來保存我的應用程序線程。Netty客戶端在I/O客戶端上的優勢?

我寫了一個演示客戶端,請找到下面的代碼。期望一個線程可以連接到不同的端口來處理它們,但我錯了。 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客戶的好處是什麼?它保存線程嗎?

回答

1

Netty保存線程。當同步等待打開和關閉連接時,NettyClient會浪費線程(調用awaitUninterruptibly())。

順便說一句你的客戶有多少個連接?也許使用經典的同步單線程每連接方法就足夠了?通常我們必須在服務器端保存線程。

0

Netty允許您處理成千上萬的連接與幾個線程。 在客戶端應用程序中使用時,它允許少數線程與服務器建立數千個併發連接。

你已經把睡眠()在你的線程。我們必須從來沒有阻止Netty工人/老闆的線程。即使需要執行任何一次性阻止操作,也必須將其卸載到另一個執行程序。 Netty使用NIO,同一線程可用於創建新連接,而較早的連接在其輸入緩衝區中獲取一些數據。